计算数据系列的百分比
Calculate percentage of a data series
我有一系列数据,我想计算与第一个日期相比的增长百分比值
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
你可以试试这个:-
data = pd.DataFrame({'date':[1,2,3,4,5,6], 'price':[5,4,8,10,11,12]})
data['price'] = data['price'].transform(lambda x:((x-data['price'].iloc[0])/data['price'].iloc[0])*100).astype(int)
print(data)
输出:-
date price
0 1 0
1 2 -20
2 3 60
3 4 100
4 5 120
5 6 140
你可以像这样使用字典:
d = {1: 5,
2: 4,
3: 8,
4: 10,
5: 11,
6: 12}
d2 = {k:((d[k]-5)/5)*100 for k in d.keys()}
print(d2)
输出:
{1: 0.0, 2: -20.0, 3: 60.0, 4: 100.0, 5: 120.0, 6: 140.0}
如果你不想要小数点:
d = {1: 5,
2: 4,
3: 8,
4: 10,
5: 11,
6: 12}
d2 = {k:int(((d[k]-5)/5)*100) for k in d.keys()}
print(d2)
输出:
{1: 0, 2: -20, 3: 60, 4: 100, 5: 120, 6: 140}
我有一系列数据,我想计算与第一个日期相比的增长百分比值
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
你可以试试这个:-
data = pd.DataFrame({'date':[1,2,3,4,5,6], 'price':[5,4,8,10,11,12]})
data['price'] = data['price'].transform(lambda x:((x-data['price'].iloc[0])/data['price'].iloc[0])*100).astype(int)
print(data)
输出:-
date price
0 1 0
1 2 -20
2 3 60
3 4 100
4 5 120
5 6 140
你可以像这样使用字典:
d = {1: 5,
2: 4,
3: 8,
4: 10,
5: 11,
6: 12}
d2 = {k:((d[k]-5)/5)*100 for k in d.keys()}
print(d2)
输出:
{1: 0.0, 2: -20.0, 3: 60.0, 4: 100.0, 5: 120.0, 6: 140.0}
如果你不想要小数点:
d = {1: 5,
2: 4,
3: 8,
4: 10,
5: 11,
6: 12}
d2 = {k:int(((d[k]-5)/5)*100) for k in d.keys()}
print(d2)
输出:
{1: 0, 2: -20, 3: 60, 4: 100, 5: 120, 6: 140}