[英]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 ![]() |
---|---|---|---|
0 ![]() |
checking_balance![]() |
1000 non-null ![]() |
category![]() |
1 ![]() |
months_loan_duration ![]() |
1000 non-null ![]() |
int64![]() |
2 ![]() |
credit_history![]() |
1000 non-null ![]() |
category![]() |
3 ![]() |
purpose![]() |
1000 non-null ![]() |
category![]() |
4 ![]() |
amount![]() |
1000 non-null ![]() |
int64![]() |
5 ![]() |
savings_balance![]() |
1000 non-null ![]() |
category![]() |
6 ![]() |
employment_duration![]() |
1000 non-null ![]() |
category![]() |
7 ![]() |
percent_of_income![]() |
1000 non-null ![]() |
int64![]() |
8 ![]() |
years_at_residence![]() |
1000 non-null ![]() |
int64![]() |
9 ![]() |
age![]() |
1000 non-null ![]() |
int64![]() |
10 ![]() |
other_credit![]() |
1000 non-null ![]() |
category![]() |
11 ![]() |
housing![]() |
1000 non-null ![]() |
category![]() |
12 ![]() |
existing_loans_count ![]() |
1000 non-null ![]() |
int64![]() |
13 ![]() |
job![]() |
1000 non-null ![]() |
category![]() |
14 ![]() |
dependents![]() |
1000 non-null ![]() |
int64![]() |
15 ![]() |
phone![]() |
1000 non-null ![]() |
category![]() |
16 ![]() |
default![]() |
1000 non-null ![]() |
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.