python – 将每一行除以前一行

我有pandas数据帧:

df = pd.DataFrame()
df['city'] = ['NY','NY','LA','LA']
df['hour'] = ['0','12','0','12']
df['value'] = [12,24,3,9]

   city hour value
0   NY  0   12
1   NY  12  24
2   LA  0   3
3   LA  12  9

我希望,对于每个城市,将每一行除以前一行,并将结果写入新的数据帧.所需的输出是:

city ratio
NY   2
LA   3

什么是最pythonic的方式来做到这一点?

解决方法:

首先除以每组shift个值:

df['ratio'] = df['value'].div(df.groupby('city')['value'].shift(1))
print (df)
  city hour  value  ratio
0   NY    0     12    NaN
1   NY   12     24    2.0
2   LA    0      3    NaN
3   LA   12      9    3.0

然后删除NaN并仅选择city和ratio列:

df = df.dropna(subset=['ratio'])[['city', 'ratio']]
print (df)
  city  ratio
1   NY    2.0
3   LA    3.0

相关文章

转载:一文讲述Pandas库的数据读取、数据获取、数据拼接、数...
Pandas是一个开源的第三方Python库,从Numpy和Matplotlib的基...
整体流程登录天池在线编程环境导入pandas和xrld操作EXCEL文件...
 一、numpy小结             二、pandas2.1为...
1、时间偏移DateOffset对象DateOffset类似于时间差Timedelta...
1、pandas内置样式空值高亮highlight_null最大最小值高亮背景...