PANDA VALUE_COUNTS包含GROUP BY之前的所有值 [英] pandas value_counts include all values before groupby

查看:33
本文介绍了PANDA VALUE_COUNTS包含GROUP BY之前的所有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下数据帧:

df = pd.DataFrame([['a',1, -1], ['a', 1, -1], ['b', 0, -1], ['c', -1, -1]] ,columns = ['col1', 'col2', 'col3'])
df
    col1    col2    col3
0   a       1       -1
1   a       1       -1
2   b       0       -1
3   c       -1      -1
现在我想按列对df进行分组,并分别计算col1列中的值出现的次数。

groupby_df = df.groupby('col1') 
for a,b in groupby_df:
    print("{0} -> 
{1}".format(a, b['col1'].value_counts().sort_index()))

我得到:

a -> 
a    2
Name: col1, dtype: int64
b -> 
b    1
Name: col1, dtype: int64
c -> 
c    1
Name: col1, dtype: int64

但是我想单独统计出现的次数,并且仍然包括所有列值,如下所示:

a -> 
a    2
b    0
c    0
Name: col1, dtype: int64
b -> 
a    0
b    1
c    0
Name: col1, dtype: int64
c -> 
a    0
b    0
c    1
Name: col1, dtype: int64

如有任何帮助,我们将不胜感激!

推荐答案

尝试使用.reindex()

import pandas as pd

df = pd.DataFrame([['a',1, -1], ['a', 1, -1], ['b', 0, -1], ['c', -1, -1]] ,columns = ['col1', 'col2', 'col3'])

# Create index using unique values of col1.

uniques = pd.Index(df['col1'].unique())

# Group.

groupby_df = df.groupby('col1')

# Use reindex to assign and autoamtically align the value counts with the index.

for a, b in groupby_df:
    print(b['col1'].value_counts().sort_index().reindex(uniques, fill_value = 0))

给予:

a    2
b    0
c    0
Name: col1, dtype: int64
a    0
b    1
c    0
Name: col1, dtype: int64
a    0
b    0
c    1
Name: col1, dtype: int64

这篇关于PANDA VALUE_COUNTS包含GROUP BY之前的所有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆