%在数字记录中增加值

2024-10-03 19:21:37 发布

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

我的数据集

name date        record   
A    2018-09-18      95       
A    2018-10-11     104      
A    2018-10-30     230       
A    2018-11-23     124       
B    2020-01-24      95       
B    2020-02-11     167       
B    2020-03-07      78    

如您所见,有几个记录的名称和日期

与之前的记录相比,我希望看到上升最多的记录

输出我想要的内容

name record_before_date record_before record_increase_date record_increase increase_rate
A            2018-10-11           104           2018-10-30             230        121.25
B            2020-01-24            95           2020-02-11             167         75.79

我不是在比较最低和最高,但我想在下一个记录出现时检查最高上升率的记录,以及上升率

增长率公式=(记录增长-记录之前)/记录之前*100

任何帮助都将不胜感激。 谢谢你的阅读


Tags: 数据name名称内容daterate记录record
1条回答
网友
1楼 · 发布于 2024-10-03 19:21:37

使用:

#get percento change per groups
s = df.groupby("name")["record"].pct_change()
#get row with maximal percent change
df1 = df.loc[s.groupby(df['name']).idxmax()].add_suffix('_increase')
#get row with previous maximal percent change
df2 = (df.loc[s.groupby(df['name'])
         .apply(lambda x: x.shift(-1).idxmax())].add_suffix('_before'))
#join together
df = pd.concat([df2.set_index('name_before'), 
                df1.set_index('name_increase')], axis=1).rename_axis('name').reset_index()
#apply formula
df['increase_rate'] = (df['record_increase'].sub(df['record_before'])
                                            .div(df['record_before'])
                                            .mul(100))
print (df)
  name date_before  record_before date_increase  record_increase  \
0    A  2018-10-11            104    2018-10-30              230   
1    B  2020-01-24             95    2020-02-11              167   

   increase_rate  
0     121.153846  
1      75.789474  

相关问题 更多 >