код ниже - мой оригинальный способ.
import pandas as pd
data = {'id':[1001,1001,1001,1001,1001,1001,1001,1001,1002,1002,1002,1002,1002,1002,1002,1002],
'name':['Tom', 'Tom', 'Tom', 'Tom','Tom', 'Tom', 'Tom', 'Tom','Jack','Jack','Jack','Jack','Jack','Jack','Jack','Jack'],
'team':['A','A', 'B', 'B', 'C','C', 'D', 'D','A','A', 'B', 'B', 'C','C', 'D', 'D',],
'year':[2011,2011,2012,2012,2013,2013,2014,2014,2011,2011,2012,2012,2013,2013,2014,2014],
'avg':[0.500,0.400,0.300,0.200,0.100,0.200,0.300,0.400,0.500,0.400,0.300,0.200,0.100,0.200,0.300,0.400]}
df = pd.DataFrame(data)
print (df)
team_names = [c for c in df['team'].value_counts().index]
team_names
for i in team_names:
df[i+'_vs_avg_2011'] = df.loc[(df['team']==i)&(df['year']==2011)].groupby(['id','name'])['avg'].transform('mean')
df[i+'_vs_avg_2012'] = df.loc[(df['team']==i)&(df['year']==2012)].groupby(['id','name'])['avg'].transform('mean')
df[i+'_vs_avg_2013'] = df.loc[(df['team']==i)&(df['year']==2013)].groupby(['id','name'])['avg'].transform('mean')
df[i+'_vs_avg_2014'] = df.loc[(df['team']==i)&(df['year']==2014)].groupby(['id','name'])['avg'].transform('mean')
print(i)
для части цикла, которую я пробовал
years_from_to = [str(i).zfill(2) for i in range(2011,2014)]
years_from_to
for i,j in team_names, years_from_to:
df[i+'_vs_avg_'+j] = df.loc[(df['team']==i)&(df['year']==j)].groupby(['id','name'])['avg'].transform('mean')
print(i)
ValueError: слишком много значений для распаковки (ожидается 2)
Есть ли способ упростить это или исправить этот код?