Is there a faster way to find the length of the longest string in a Pandas DataFrame than what\'s shown in the example below?
import numpy as np
import panda         
        
Excellent answers, in particular Marius and Ricky which were very helpful.
Given that most of us are optimising for coding time, here is a quick extension to those answers to return all the columns' max item length as a series, sorted by the maximum item length per column:
mx_dct = {c: df[c].map(lambda x: len(str(x))).max() for c in df.columns}
pd.Series(mx_dct).sort_values(ascending =False)
Or as a one liner:
pd.Series({c: df[c].map(lambda x: len(str(x))).max() for c in df).sort_values(ascending =False)
Adapting the original sample, this can be demoed as:
import pandas as pd
x = [['ab', 'bcd'], ['dfe', 'efghik']]
df = pd.DataFrame(x, columns=['col1','col2'])
print(pd.Series({c: df[c].map(lambda x: len(str(x))).max() for c in df}).sort_values(ascending =False))
Output:
col2    6
col1    3
dtype: int64