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
Sometimes you want the length of the longest string in bytes. This is relevant for strings that use fancy Unicode characters, in which case the length in bytes is greater than the regular length. This can be very relevant in specific situations, e.g. for database writes.
df_col_len = int(df[df_col_name].str.encode(encoding='utf-8').str.len().max())
The above line has the extra str.encode(encoding='utf-8'). The output is enclosed in int() because it is otherwise a numpy object.