Pandas Learning-1
2016, Sep 12
Data
df=
Branch | Buyer | Date | Quantity | |
---|---|---|---|---|
0 | A | Carl | 2013-01-01 13:00:00 | 1 |
1 | A | Mark | 2013-01-01 13:05:00 | 3 |
2 | A | Carl | 2013-10-01 20:00:00 | 5 |
3 | A | Carl | 2013-10-02 10:00:00 | 1 |
4 | A | Joe | 2013-10-01 20:00:00 | 8 |
5 | A | Joe | 2013-10-02 10:00:00 | 1 |
6 | A | Joe | 2013-12-02 12:00:00 | 9 |
7 | B | Carl | 2013-12-02 14:00:00 | 3 |
Panda Grouper
Parameters:
-
key : string, defaults to None.
groupby key, which selects the grouping column of the target
-
level : name/number, defaults to None
the level for the target index
-
freq : string / frequency object, defaults to None
This will groupby the specified frequency if the target selection (via key or level) is a datetime-like object. For full specification of available frequencies, please see here.
- axis : number/name of the axis, defaults to 0
-
sort : boolean, default to False
whether to sort the resulting labels
- additional kwargs to control time-like groupers (when freq is passed)
- closed : closed end of interval; left or right
- label : interval boundary to use for labeling; left or right
- convention : {‘start’, ‘end’, ‘e’, ‘s’} If grouper is PeriodIndex
Returns: A specification for a groupby instruction
Example:
df.groupby([pd.Grouper(freq='1M',key='Date'),'Buyer']).sum()
Return:
Date | Buyer | Quantity |
---|---|---|
2013-01-31 | Carl | 1 |
Mark | 3 | |
2013-10-31 | Carl | 6 |
Joe | 9 | |
2013-12-31 | Carl | 3 |
Joe | 9 |