'Looping through pandas value_counts()

I'm manually looking for all values in my df columns like this (to search for weird entries):

df['sex'].value_counts(), df['famsize'].value_counts(), df['Pstatus'].value_counts(), df['traveltime'].value_counts()...

then i get:

(F    591
 M    453
 Name: sex, dtype: int64,
 GT3    738
 LE3    306
 Name: famsize, dtype: int64,
 T    923
 A    121
 Name: Pstatus, dtype: int64,
 1    623
 2    320
 3     77
 4     24
(...)

But there are so many columns, so I tried to loop through it :

for v in df.columns:
    df[v].value_counts()

but nothing is returned, I also tried:

for v in df.columns:
    df[v].value_counts().apply(display)

and:

for v in df.columns:
    df[v].value_counts().apply(print)

but it returns a long list of numbers without the indexes/labels.

Is there another way to automatically look through my dfs values? Maybe still using loop for.



Solution 1:[1]

I would just do:

for v in df.columns:
    print(df[v].value_counts())

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 gioarma