2017-04-25 31 views
1
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是具有數據這樣熊貓想之間兩種不同的數據使用DIC

即)2015-01 =(111分之654)* 100

我wnat到計算百分比計算像同年個月的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......} 
+0

'(x/y)* 100'不起作用? – jezrael

+0

'2015-01 =(654/111)* 100'是否正確? – jezrael

+0

您可以添加'2015-02 =(654 + 654/456)* 100 2015-03 =(654 + 654 + 315/1231)* 100'的輸出嗎? – jezrael

回答

1

看來你需要sort_index + cumsum + div + mul + iloc(用於刪除第一個值)+ to_dict

#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} 
+0

我編輯了我的問題,你可以再檢查一次嗎? –

+0

請檢查編輯答案。 – jezrael

+0

如何把字典中的x? –

相關問題