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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With