Python %在数字记录中增加值

Python %在数字记录中增加值,python,pandas,numpy,compare,Python,Pandas,Numpy,Compare,我的数据集 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 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

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

使用:

#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  

您好@jezrael,我在第2行遇到一个错误,
“传递列表喜欢。不再支持带有任何缺少标签的loc或[],请参阅https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#deprecate-loc reindex列表状'
@ybin-可以使用
df=df.reset\u索引(drop=True)
在此解决方案之前?@ybin-如果某个组只有1行或2行,会发生什么?@ybin-已测试且错误表示有某个组只有一行。哦,我知道我会检查