How to use pandas group-by when adding new columns at the same level?

The source data is as follows:

    Date        E   
0   2017-09-01  -   
1   2017-09-01  +   
2   2017-09-01  +   
3   2017-09-01  +  
...
... 

After applying groupby:

df.groupby(['Date', 'E'])['Date'].count().to_frame(name = 'Count').reset_index()

I get a DataFrame that looks like this:

    Date        E   Count
0   2017-09-01  +   11
1   2017-09-01  -   1
2   2017-09-04  +   1
3   2017-09-04  -   7
4   2017-09-05  +   1
5   2017-09-05  -   23

How can I convert this to a dataframe that looks like this:

    Date        +   -
0   2017-09-01  11  1
2   2017-09-04  1   7
4   2017-09-05  1   23
+4
source share
2 answers

I think it’s better to use GroupBy.sizebecause it is GroupBy.countused for count values NaN.

Then change the form unstack:

df = df.groupby(['Date', 'E'])['Date'].size().unstack(fill_value=0).reset_index()
print (df)
E        Date  +  -
0  2017-09-01  3  1

A less typing solution, but in a larger df slowier crosstab:

df = pd.crosstab(df['Date'], df['E'])
print (df)
E           +  -
Date            
2017-09-01  3  1
+4
source

Or use pd.crosstab

In [1736]: pd.crosstab(df.Date, df.E)
Out[1736]:
E           +  -
Date
2017-09-01  3  1
2017-09-02  1  0

Or, pivot_table

In [1737]: pd.pivot_table(df, index=['Date'], columns=['E'], aggfunc=len, fill_value=0)
Out[1737]:
E           +  -
Date
2017-09-01  3  1
2017-09-02  1  0
+4
source

Source: https://habr.com/ru/post/1686054/


All Articles