简体   繁体   中英

Pivoting a two-column DataFrame into value counts by date

I have a DataFrame that is indexed by a default range. It has two columns: "date" and "type".

         date  type
0  2019-01-01     A
1  2019-01-02     C
2  2019-01-03     A
3  2019-01-01     B
4  2019-01-01     A
5  2019-01-02     B
6  2019-01-02     B
7  2019-01-03     C
8  2019-01-03     A
9  2019-01-01     B

My desired end result is a DataFrame like this:

      date  A  B  C
2019-01-01  2  2  0
2019-01-02  0  2  1
2019-01-03  2  0  1

I'm decently close with this:

df.pivot_table(index='date', columns='type', aggfunc={'type': 'count'}, fill_value=0)

But it produces this strange result that I can't figure out how to interpret:

               type
      type  A  B  C
      date
2019-01-01  2  2  0
2019-01-02  0  2  1
2019-01-03  2  0  1

Any clues on what I'm missing? It seems that this should be rather straightforward.

Use DataFrame.rename_axis to remove the name object of the column index axis:

df = df.pivot_table(index='date', 
                    columns='type', 
                    values='type', 
                    aggfunc='size', 
                    fill_value=0).rename_axis(None, axis='columns')

            A  B  C
date               
2019-01-01  2  2  0
2019-01-02  0  2  1
2019-01-03  2  0  1

Which would be same as:

df = df.pivot_table(index='date', 
                    columns='type', 
                    values='type', 
                    aggfunc='size', 
                    fill_value=0)

df.columns.name = None

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM