原始 Dataframe
Parent Family Name Money A Money B
Jack Everdeen 10000 20000
Rita Lawrence 4000 2000
Amy Donnie 5000 9000
我想要的。
Parent Family Name Money Money
Jack Everdeen Money A 10000
Jack Everdeen Money B 20000
Rita Lawrence Money A 4000
Rita Lawrence Money B 2000
Amy Donnie Money A 5000
Amy Donnie Money B 9000
我尝试了它与透视功能,但它不工作。我的代码如下。
data_1 = [['Jack','Everdeen',10000,20000],['Rita','Lawrence',4000,2000],['Amy','Donnie',5000,9000]]
df_1 = pd.DataFrame(data_1,columns=['Parent','Family Name','Money A','Money B'],dtype=float)
print(df_1)
loading_pivot_table = pd.pivot_table(df_1
,index = ['Parent','Family Name']
,columns = ['Parent','Family Name']
,values = ['Money A','Money B']
,fill_value = 0)
#convert pivot table to DataFrame
loading_pivot_table_convert = loading_pivot_table.reset_index()
print(loading_pivot_table_convert)
2条答案
按热度按时间zaq34kh61#
Melt它
0md85ypi2#
你试过集合索引吗?