我有一个像这样的数据框,其中包含重复的列名:ID 作为索引加载
JANUARY FEBRUARY MARCH
ID Sales Revenue Sales Revenue Sales Revenue
03 10.00 5.00 0.00 0.00 10.00 19.00
05 20.00 20.00 20.00 20.00 20.00 20.00
06 30.00 30.00 30.00 30.00 30.00 30.00
07 30.00 30.00 30.00 30.00 30.00 30.00
我想将其显示如下:
ID Sales Revenue
03 10.00 5.00
05 20.00 20.00
06 30.00 30.00
07 30.00 30.00
03 0.00 0.00
05 20.00 20.00
06 30.00 30.00
07 30.00 30.00
03 10.00 19.00
05 20.00 20.00
06 30.00 30.00
07 30.00 30.00
目前我正在使用,但期待更好的方法。我尝试过熔化,但这仅适用于一列:
cols = df.columns.to_list()
for i in range(1, len(cols), 2): # #Loading each month's data to the data frame
sub_cols = cols[i:i + 2]
sub_cols .insert(0, cols[0])
sub_df = df.filter(sub_cols , axis=1)
sub_df.columns = ['ID', 'Revenue', 'Sales']
if i == 1:
final_df = sub_df
else:
final_df = final_df.append(sub_df)
森林海
大话西游666
相关分类