如何打印数据框中所有列的值计数

2024-07-01 08:27:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个数据集,其中有24个因变量。我想计算所有列中每个值(类)的出现次数

我使用了以下代码:

for i in target_c:
    print(f'{target_col[i].value_counts}')

输出如下:

<bound method IndexOpsMixin.value_counts of 0           0
1           0
2           0
3           0
4           0
           ..
13647304    0
13647305    0
13647306    0
13647307    0
13647308    0
Name: saving_account, Length: 13647309, dtype: int64>
<bound method IndexOpsMixin.value_counts of 0           0
1           0
2           0
3           0
4           0
           ..
13647304    0
13647305    0
13647306    0
13647307    0
13647308    0

所有列的预期输出都是这样的

saving_account
0                 13645913
1                     1396
dtype: int64

Tags: of数据代码targetvalueaccount次数method
2条回答

正如@karlknechtel所说,应该从dataframe对象调用value_counts。因此,如果数据帧是df,则应使用:

df.value_counts()

按“可用类别”列分组。将这些列相加

import pandas as pd
df  = pd.read_csv('sample.csv')
df.groupby(['Target']).sum()

相关问题 更多 >

    热门问题