我有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