我想在数据框中重新排列名称(如下所示),但要保留其平均分数。所以我知道我可以执行以下操作以获取每个人的值的列表:
df1.groupby('Person1')['AverageScoreP1'].mean()
df1.groupby('Person2')['AverageScoreP2'].mean()
但是如何将它们以正确的顺序放入新的数据帧(df2)中。我曾经使用过'transform'方法,但没有将名称和它们的平均分数作为一个实体附加。相反,它只是按照相同的顺序将值从df1复制到df2(如下所示,显示在df2(Attempted)中)。我敢肯定这有一个简单的解决方案!
df1:
Person 1 Person 2 AverageScoreP1 AverageScoreP2
Sam Ellie 2.3 10.2
James Sarah 4.5 8.8
Steven Jane 6.6 3.5
Martin Becky 7.7 2.0
df2:
我想显示的内容:
Person 1 Person 2 AverageScoreP1 AverageScoreP2
Martin Sarah 7.7 8.8
Steven Becky 6.6 2.0
Sam Ellie 2.3 10.2
James Jane 4.5 3.5
df2(尝试):
Person 1 Person 2 AverageScoreP1 AverageScoreP2
Martin Sarah 2.3 10.2
Steven Becky 4.5 8.8
Sam Ellie 6.6 3.5
James Jane 7.7 2.0
答案 0 :(得分:0)
def search_score1(person1):
return df1[df1.Person1==person1].AverageScoreP1
def search_score2(person2):
return df1[df1.Person2==person2].AverageScoreP2,
df2['search_score1']==df2.apply(lambda x:search_score1(x.Person1),axis=1)
df2['search_score2']==df2.apply(lambda x:search_score2(x.Person2),axis=1)
类似的事情会解决问题
答案 1 :(得分:0)
这是一种方法(内嵌评论):
df1.columns=df1.columns.str.replace(' ','') #replace spaces, if required create a copy df
d=dict(pd.wide_to_long(df1.reset_index(),['Person','AverageScoreP'],
i='index',j='v').values) #create a dict of name and avg score
m=df2.replace(d) #replace df2 with dict
m.columns=[i for i in df1.columns if 'Person' not in i] #change col names
df2=df2.join(m) #join with df2
Person 1 Person 2 AverageScoreP1 AverageScoreP2
0 Martin Sarah 7.7 8.8
1 Steven Becky 6.6 2.0
2 Sam Ellie 2.3 10.2
3 James Jane 4.5 3.5