大熊猫想要计算两个不同数据之间使用dic的百分比

时间:2017-04-25 17:29:09

标签: python pandas

dt=df['dt'] 
first=df['first']
x=pd.value_counts(pd.to_datetime(df.dt.sum()).strftime('%Y-%m'))
y=pd.value_counts(pd.to_datetime(first.sum()).strftime('%Y-%m'))

x                    y
2015-01 111          2015-01  654
2015-03 1231         2015-03  315
2015-05 123          2015-05  464
2015-02 456          2015-02 654
.                    .
.                    .

x和y有这样的数据

ie)2015-01 =(654/111)* 100

我想像年同月x,y(x / y)* 100

一样计算
  y         x
((2015-01+2015-02)/2015-02)*100
((2015-01+2015-02+2015-03)/2015-03)*100

2015-02=(654+654/456)*100
2015-03=(654+654+315/1231)*100
like this


I want to put this x and y in dictionary
date is key and number is value

ie) dict={2105-01:111,2015-03:1231......}

1 个答案:

答案 0 :(得分:1)

您似乎需要sort_index + cumsum + div + mul + iloc(删除第一个值)+ to_dict:< / p>

#last value is incorrect, because 2015-04 is not in sample
d = y.sort_index().cumsum().div(x).mul(100).iloc[1:].to_dict()
print (d)
{'2015-02': 286.84210526315786, 
 '2015-05': 1696.747967479675, 
 '2015-03': 131.84402924451666}