我有一系列数据,我想计算与第一个日期相比的增长百分比值
Date: 1 2 3 4 5 6 .....
Price: 5 4 8 10 11 12
从日期 2 开始,我想做 ((4-5)/5)*100 即 -20。日期 3 是 ((8-5)/5)*100,即 100%。所以会变成这样
Date: 1 2 3 4 5 6 .....
Price: 0 -20 60 100 120 140
谢谢
你可以这样做:
import pandas as pd
df = pd.DataFrame({"Date": [1, 2, 3, 4, 5, 6],
"Price": [5, 4, 8, 10, 11, 12]})
date1 = df.loc[0, "Price"] # price of first date
df["Price"] = df["Price"].apply(lambda x: ((x-date1)/date1)*100)
print(df)
# Date Price
#0 1 0.0
#1 2 -20.0
#2 3 60.0
#3 4 100.0
#4 5 120.0
#5 6 140.0
这是为了与下面评论中发布的 OP 的 CSV 文件完全匹配:
df = pd.read_csv("fb_removed.csv", skiprows=1, header=None).reset_index(drop=True)
df = df.T.loc[1:]
df.columns = ["Date", "Price"]
date1 = float(df.iloc[0]["Price"]) # price of first date
df["Price"] = df["Price"].apply(lambda x: ((float(x)-date1)/date1)*100)
print(df.head())
# Date Price
#1 2019/6/12 0.000000
#2 2019/6/13 1.388259
#3 2019/6/14 3.593469
#4 2019/6/17 7.981034
#5 2019/6/18 7.672537
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句