简体   繁体   English

遍历 Python 中 dataframe 的列并显示分类变量的值计数

[英]Iterate through the columns of a dataframe in Python and show value counts for the categorical variavles

I'm new to Python and programming, so this is no doubt a newbie question.我是 Python 和编程的新手,所以这无疑是一个新手问题。 I want to show the value counts for each unique value of each categorical variable in a data frame, but what I've written isn't working.我想显示数据框中每个分类变量的每个唯一值的值计数,但我写的内容不起作用。 I'm trying to avoid writing separate lines for each individual column if I can help it.如果可以的话,我尽量避免为每个单独的列写单独的行。

# # Column柱子 Non-Null Count非空计数 Dtype D型
0 0 checking_balance检查余额 1000 non-null 1000 非空 category类别
1 1个 months_loan_duration months_loan_duration 1000 non-null 1000 非空 int64整数64
2 2个 credit_history信用记录 1000 non-null 1000 非空 category类别
3 3个 purpose目的 1000 non-null 1000 非空 category类别
4 4个 amount数量 1000 non-null 1000 非空 int64整数64
5 5个 savings_balance储蓄余额 1000 non-null 1000 非空 category类别
6 6个 employment_duration就业_持续时间 1000 non-null 1000 非空 category类别
7 7 percent_of_income收入百分比 1000 non-null 1000 非空 int64整数64
8 8个 years_at_residence居住年数 1000 non-null 1000 非空 int64整数64
9 9 age年龄 1000 non-null 1000 非空 int64整数64
10 10 other_credit其他信用 1000 non-null 1000 非空 category类别
11 11 housing住房 1000 non-null 1000 非空 category类别
12 12 existing_loans_count existing_loans_count 1000 non-null 1000 非空 int64整数64
13 13 job工作 1000 non-null 1000 非空 category类别
14 14 dependents家属 1000 non-null 1000 非空 int64整数64
15 15 phone电话 1000 non-null 1000 非空 category类别
16 16 default默认 1000 non-null 1000 非空 category类别

Code I've written:我写的代码:

for col in creditData.columns:
    if creditData[col].dtype == 'category':
        print(creditData[col].value_counts())

The results:结果:

unknown       394
< 0 DM        274
1 - 200 DM    269
> 200 DM       63
Name: checking_balance, dtype: int64

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-14-6a53236835fc> in <module>
      1 for col in creditData.columns: # Loop through all columns in the dataframe
----> 2         if creditData[col].dtype == 'category':
      3                 print(creditData[col].value_counts())

TypeError: data type 'category' not understood

this works for me这对我有用

for i in creditData.columns:
    if creditData[i].dtype != 'int64':
        print(creditData[i].value_counts())

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

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