Reputation: 29
is there a way to obtain the max length for each column in a dataframe?. I know how to do it one by one with object/numeric type, but the problem comes out when there is so much columns which i have to and also de type of each column (object/int/float/date).
Example:
a b c
1 aad sdsd ds
2 sds wewe 5
Max a= 3 ; b=4 ;c=2
Upvotes: 1
Views: 1735
Reputation: 261860
You could apply
str.len
per column and get the max
:
df.astype(str).apply(lambda s: s.str.len()).max()
output:
a 3
b 4
c 4
dtype: int64
As dictionary:
d = df.astype(str).apply(lambda s: s.str.len()).max().to_dict()
output: {'a': 3, 'b': 4, 'c': 4}
Or using a dictionary comprehension:
d = {k: df[k].str.len().max() for k in df}
Upvotes: 1