Find length of longest string in Pandas dataframe column

前端 未结 5 663
忘掉有多难
忘掉有多难 2020-12-23 13:18

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         


        
5条回答
  •  甜味超标
    2020-12-23 13:52

    Just as a minor addition, you might want to loop through all object columns in a data frame:

    for c in df:
        if df[c].dtype == 'object':
            print('Max length of column %s: %s\n' %  (c, df[c].map(len).max()))
    

    This will prevent errors being thrown by bool, int types etc.

    Could be expanded for other non-numeric types such as 'string_', 'unicode_' i.e.

    if df[c].dtype in ('object', 'string_', 'unicode_'):
    

提交回复
热议问题