Python 如何用当前行计算新行,用数据帧除以最后一行?
我要纽普莱斯=Python 如何用当前行计算新行,用数据帧除以最后一行?,python,pandas,dataframe,Python,Pandas,Dataframe,我要纽普莱斯= price = price 0 111 1 222 2 333 3 444 用于: 使用pandas的shift功能,将索引移动所需的周期数。例如,如果您的变量是price=pd.DataFrame([111222333444]),那么您将通过price/price.shift(句点=1,填充值=1)获得所需的结果。 price 0 111 1 222/111 2 333/222 3 444/333 In [1046]: df['new_price'] = df.
price =
price
0 111
1 222
2 333
3 444
用于:
使用pandas的
shift
功能,将索引移动所需的周期数。例如,如果您的变量是price=pd.DataFrame([111222333444])
,那么您将通过price/price.shift(句点=1,填充值=1)获得所需的结果。
price
0 111
1 222/111
2 333/222
3 444/333
In [1046]: df['new_price'] = df.price.div(df.price.shift()).fillna(df.price)
In [1047]: df
Out[1047]:
price new_price
0 111 111.000000
1 222 2.000000
2 333 1.500000
3 444 1.333333