我构建了以下DataFrame:
Movies Cost Tickets
0 1158 0.000000 2.000000
1 1158 0.000000 0.000000
2 1158 0.000000 0.000000
3 1158 0.000000 3.000000
我已经使用stack()将配置更改为:
Event 1
0 Movies 1158
1 Cost 0.000000
2 Tickets 2.000000
3 Movies 1158
4 Cost 0.000000
5 Tickets 0.000000
6 Movies 1158
7 Cost 0.000000
8 Tickets 0.000000
9 Movies 1158
10 Cost 0.000000
11 Tickets 3.000000
但这是彼此堆叠的数据,我一直在寻找一个新列,最终目标是:
Event 1 Event 2 Event 3 Event 4
0 Movies 1158 1158 1158 1158
1 Cost 0.000000 0.000000 0.000000 0.000000
2 Tickets 2.000000 0.000000 0.000000 3.000000
这是我当前的配置:
df = pd.DataFrame({
'Tickets': pd.Series(Tickets),
'Movies': pd.Series(Movies),
'Cost': pd.Series(costs)})
print(df)
df.columns.name = ''
stackEvent3 = df.stack()
stackEvent3 = df.stack().reset_index(level=0, drop=True).reset_index(name='Event1')
非常感谢任何指导,谢谢!
答案 0 :(得分:2)
将DataFrame.transpose
与DataFrame.add_prefix
一起使用:
new_df = df.T.rename(columns=dict(zip(df.index, df.index+1))).add_prefix('Event ')
Event 1 Event 2 Event 3 Event 4
Movies 1158.0 1158.0 1158.0 1158.0
Cost 0.0 0.0 0.0 0.0
Tickets 2.0 0.0 0.0 3.0
根据需要设置列名DataFrame.rename
,以构建一个替换字典:
df.T.rename(columns=dict(zip(df.index, ['brown', 'purple', 'yellow', 'blue'])))
或
new_df = df.T
new_df.columns = ['brown', 'purple', 'yellow', 'blue']