pandas-groupby

How can I Group By Month from a Date field using Python/Pandas

為{幸葍}努か 提交于 2020-11-30 04:58:12
问题 I have a Data-frame df which is as follows: | date | Revenue | |-----------|---------| | 6/2/2017 | 100 | | 5/23/2017 | 200 | | 5/20/2017 | 300 | | 6/22/2017 | 400 | | 6/21/2017 | 500 | I need to group the above data by month to get output as: | date | SUM(Revenue) | |------|--------------| | May | 500 | | June | 1000 | I tried this code but it did not work: df.groupby(month('date')).agg({'Revenue': 'sum'}) I want to only use Pandas or Numpy and no additional libraries 回答1: try this: In [6]:

Adding Subtotals to Pandas Groupby

南楼画角 提交于 2020-11-27 03:05:22
问题 I am looking for a cleaner way to add subtotals to Pandas groupby. Here is my DataFrame: df = pd.DataFrame({ 'Category':np.random.choice( ['Group A','Group B'], 50), 'Sub-Category':np.random.choice( ['X','Y'], 50), 'Product':np.random.choice( ['Product 1','Product 2'], 50), 'Units_Sold':np.random.randint(1,100, size=(50)), 'Dollars_Sold':np.random.randint(100,1000, size=50), 'Date':np.random.choice( pd.date_range('1/1/2011','03/31/2011', freq='D'), 50, replace=False)}) From there, I create a

Adding Subtotals to Pandas Groupby

拟墨画扇 提交于 2020-11-27 03:04:47
问题 I am looking for a cleaner way to add subtotals to Pandas groupby. Here is my DataFrame: df = pd.DataFrame({ 'Category':np.random.choice( ['Group A','Group B'], 50), 'Sub-Category':np.random.choice( ['X','Y'], 50), 'Product':np.random.choice( ['Product 1','Product 2'], 50), 'Units_Sold':np.random.randint(1,100, size=(50)), 'Dollars_Sold':np.random.randint(100,1000, size=50), 'Date':np.random.choice( pd.date_range('1/1/2011','03/31/2011', freq='D'), 50, replace=False)}) From there, I create a

python pandas groupby calculate change

折月煮酒 提交于 2020-11-27 01:52:03
问题 I want to calculate the value change by group. 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 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. I am not sure how to approach it, since in Group A the values initially