简体   繁体   English

按日期将两列 DataFrame 转换为值计数

[英]Pivoting a two-column DataFrame into value counts by date

I have a DataFrame that is indexed by a default range.我有一个按默认范围索引的 DataFrame。 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:我想要的最终结果是这样的 DataFrame:

      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:使用DataFrame.rename_axis删除列索引轴的name对象:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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