Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check for dataframe columns containing more than 64 characters

Tags:

python

pandas

I am trying to find the column in the Pandas dataframe that has more than 64 characters.

The dataframe has 20 columns. I want to check each value in the column for its character length and if any value is more than 64 characters, print the column name.

When I use the below code, it does not give any error, but does not output the column name which has values matching the condition.

for col in df.columns:
    if (df[col].str.len()).any() > 64:
        print col

I have also made sure that all the datatypes in the dataframe are of String type.

How can I achieve this using Pandas?

like image 692
Raj Avatar asked Sep 19 '25 18:09

Raj


1 Answers

if you want to use any, then it has to be around the comparison, such as:

if (df[col].str.len() > 64).any()

but you can also just compare to the max:

if (df[col].str.len()).max() > 64

Both should give the same result

like image 141
Ben.T Avatar answered Sep 22 '25 06:09

Ben.T