Функция Pandas Melt (), измените формат DataFrame с широкого на длинный
# pandas melt() function, change the DataFrame format from wide to long
import pandas as pd
df = pd.DataFrame({'Name': ['Bob', 'John', 'Foo', 'Bar', 'Alex', 'Tom'],
'Math': ['A+', 'B', 'A', 'F', 'D', 'C'],
'English': ['C', 'B', 'B', 'A+', 'F', 'A'],
'Age': [13, 16, 16, 15, 15, 13]})
df
#Use Melt
df1 = df.melt(id_vars=['Name', 'Age'], var_name='Subject', value_name='Grades')
df1
print(df1)
Impossible Impala