Find length of longest string in Pandas dataframe column

前端 未结 5 664
忘掉有多难
忘掉有多难 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条回答
  •  梦毁少年i
    2020-12-23 13:57

    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
    

提交回复
热议问题