按列分组DataFrame,对成员执行操作,并在新DataFram中输出结果

2024-06-17 01:01:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一些DataFrame

df = pd.DataFrame({'type':['Apple', 'Apple', 'Apple'],
                   'subtype':['Fuji', 'Cortland', 'Ambrosia'],
              'score':[1,5,10], 
              'distance':[25,50,75]}) 

我想将此数据帧按type分组,计算所有成员的distance比率(例如25/50、25/75、50/75),并在新的数据帧中返回此操作的输出,例如:

enter image description here


Tags: 数据appledataframedftype成员比率distance
1条回答
网友
1楼 · 发布于 2024-06-17 01:01:32

我认为您需要首先通过type交叉连接,然后通过^{}!=)和^{}删除具有相同值subtypes的行,最后使用^{}创建比率列,并使用^{}除法:

df = df.merge(df, on='type')
df = (df[df['subtype_x'].ne(df['subtype_y'])]
              .assign(ratio=df['distance_x'].div(df['distance_y'])))
print (df)

    type subtype_x  score_x  distance_x subtype_y  score_y  distance_y  \
1  Apple      Fuji        1          25  Cortland        5          50   
2  Apple      Fuji        1          25  Ambrosia       10          75   
3  Apple  Cortland        5          50      Fuji        1          25   
5  Apple  Cortland        5          50  Ambrosia       10          75   
6  Apple  Ambrosia       10          75      Fuji        1          25   
7  Apple  Ambrosia       10          75  Cortland        5          50   

      ratio  
1  0.500000  
2  0.333333  
3  2.000000  
5  0.666667  
6  3.000000  
7  1.500000

另外,如果需要删除距离列,可以使用^{}指定新列以供使用,并删除距离列:

df = df.merge(df, on='type', suffixes=('1','2'))
df = df[df['subtype1'].ne(df['subtype2'])].copy()
df['ratio'] = df.pop('distance1').div(df.pop('distance2'))
print (df)
    type  subtype1  score1  subtype2  score2     ratio
1  Apple      Fuji       1  Cortland       5  0.500000
2  Apple      Fuji       1  Ambrosia      10  0.333333
3  Apple  Cortland       5      Fuji       1  2.000000
5  Apple  Cortland       5  Ambrosia      10  0.666667
6  Apple  Ambrosia      10      Fuji       1  3.000000
7  Apple  Ambrosia      10  Cortland       5  1.500000

相关问题 更多 >