数据框
df_player_week_goals_means
由玩家编入索引:
GameWeek Goals Exp_Mean
Player
Pierre-Emerick Aubameyang 1 1 1.000000
Pierre-Emerick Aubameyang 2 1 1.000000
Pierre-Emerick Aubameyang 3 0 0.666667
Pierre-Emerick Aubameyang 4 1 0.750000
Pierre-Emerick Aubameyang 5 2 1.000000
... ... ... ...
Sadio Mane 23 0 0.500000
Sadio Mane 24 0 0.478261
Sadio Mane 25 0 0.458333
Sadio Mane 26 1 0.480000
Sadio Mane 27 1 0.500000
所需的输出:
我想对其进行转换,将“ DateWeek”设置为索引,删除“目标”列,并将每个唯一的玩家作为新列,最后以:
Pierre-Emerick Aubameyang Sadio Mane ...
GameWeek
1 1.000000 0.000000
2 1.000000 0.500000
3 0.666667 0.333333
4 0.750000 0.500000
5 1.000000 0.800000
...
我所能得到的是:
df_player_weekly_means = df_player_weekly_means.set_index("GameWeek").\
drop('Goals', axis=1)
打印:
Exp_Mean
GameWeek
1 1.000000
2 1.000000
3 0.666667
4 0.750000
5 1.000000
...
1 1.000000
2 1.000000
3 1.333333
4 1.500000
5 1.400000
距离我的目标还很远...
我该怎么做?
答案 0 :(得分:2)
这更像是数据透视,为此我们使用unstack
df=df_player_weekly_means.set_index("GameWeek",append=True)['Exp_Mean'].unstack(level=0)
答案 1 :(得分:2)
这是直接使用数据透视表的另一种方式。空值来自于部分数据集:
import pandas as pd
data = {
'Player':[
'Pierre-Emerick Aubameyang',
'Pierre-Emerick Aubameyang',
'Pierre-Emerick Aubameyang',
'Pierre-Emerick Aubameyang',
'Pierre-Emerick Aubameyang',
'Sadio Mane',
'Sadio Mane',
'Sadio Mane',
'Sadio Mane',
'Sadio Mane',
],
'GameWeek':[
1,2,3,4,5,
23,24,25,26,27,
],
'Goals':[
1,1,0,1,2,
0,0,0,1,1,
],
'Exp_Mean':[
1,1,0.6,0.75,1.0,
0.5,0.47,0.45,0.48,0.5,
]
}
df = pd.DataFrame(data)
df = df.set_index('Player')
print('Before')
print(df)
df = pd.pivot_table(df, index='GameWeek', columns='Player', values='Exp_Mean')
print('After')
print(df)