python pandas groupby计算变化 [英] python pandas groupby calculate change
问题描述
我想按组计算值变化.
这是我拥有的python pandas dataframe df:
This is the python pandas dataframe df I have:
Group | Date | Value
A 01-02-2016 16
A 01-03-2016 15
A 01-04-2016 14
A 01-05-2016 17
A 01-06-2016 19
A 01-07-2016 20
B 01-02-2016 16
B 01-03-2016 13
B 01-04-2016 13
C 01-02-2016 16
C 01-03-2016 16
我要计算的是,对于A组,这些值在上升,对于B组,它们在下降,对于C组,它们没有变化.
I want to calculate that for Group A, the values are going up, for Group B they are going down and for Group C they are not changing.
我不确定该如何处理,因为在A组中,该值先减小然后增大.那么,我应该看看平均变化还是最近的变化?
I am not sure how to approach it, since in Group A the values initially decrease and then increase. So should I look at the average change or most recent change?
我应该使用pct_change吗? http://pandas.pydata.org/pandas- docs/stable/generated/pandas.DataFrame.pct_change.html 我不确定如何指定时间范围.
Should I use pct_change? http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.pct_change.html I was not sure how to specify the timeframe fot that.
df.groupby.pct_change
如果我也能形象地看到它,那将是很棒的.任何建议或提示,不胜感激!谢谢
It would be great if I could visualize it too. Any advice or hint is greatly appreciated! Thank you
推荐答案
在groupby
d1 = df.set_index(['Date', 'Group']).Value
d2 = d1.groupby(level='Group').pct_change()
print(d2)
Date Group
2016-01-02 A NaN
2016-01-03 A -0.062500
2016-01-04 A -0.066667
2016-01-05 A 0.214286
2016-01-06 A 0.117647
2016-01-07 A 0.052632
2016-01-02 B NaN
2016-01-03 B -0.187500
2016-01-04 B 0.000000
2016-01-02 C NaN
2016-01-03 C 0.000000
Name: Value, dtype: float64
可视化和比较的许多方法之一是查看它们的增长方式.在这种情况下,我会
One of many ways to visualize and compare is to see how they grow. In this case, I'd
-
fillna(0)
-
add(1)
-
cumprod()
fillna(0)
add(1)
cumprod()
d2.fillna(0).add(1).cumprod().unstack().plot()
设置
setup
from io import StringIO
import pandas as pd
txt = """Group Date Value
A 01-02-2016 16
A 01-03-2016 15
A 01-04-2016 14
A 01-05-2016 17
A 01-06-2016 19
A 01-07-2016 20
B 01-02-2016 16
B 01-03-2016 13
B 01-04-2016 13
C 01-02-2016 16
C 01-03-2016 16 """
df = pd.read_clipboard(parse_dates=[1])
这篇关于python pandas groupby计算变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!