如何用熊猫提取最大长度的行

I would like to extract row which is max in Dataframe.

In following case, I would like to get id 2 row, because it includes max length 6 in B column bbbbbb.

|id|A   |B     |
|1 |abc |aaa   |
|2 |abb |bbbbbb|
|3 |aadd|cccc  |
|4 |aadc|ddddd |


|id|A   |B     |
|2 |abb |bbbbbb|

请给我一些建议。谢谢。

评论
  • 凹凸萌
    凹凸萌 回复

    您可以使用pandas字符串len和nlargest

    df['maximum'] = df.B.str.len()
    df.nlargest(1,'maximum').drop('maximum',axis=1)
    
    
        id  A   B
    1   2   abb bbbbbb
    
  • 咪咪兔
    咪咪兔 回复

    Get all columns filled by object (obviously strings) by DataFrame.select_dtypes, get length with max per rows and last filter maximal by boolean indexing:

    s = df.select_dtypes(object).applymap(len).max(axis=1)
    #if possible some missing values
    #s = df.select_dtypes(object).apply(lambda x: x.str.len()).max(axis=1)
    df1 = df[s.eq(s.max())]
    print (df1)
       id    A       B
    1   2  abb  bbbbbb
    
  • xnam
    xnam 回复

    Let's first create the DataFrame with you example:

    import pandas as pd
    
    data = {
        "id": {0: 1, 1: 2, 2: 3, 3: 4},
        "A ": {0: "abc", 1: "abb", 2: "aadd", 3: "aadc"},
        "B": {0: "aaa", 1: "bbbbbb", 2: "cccc", 3: "ddddd"}
    }
    df = pd.DataFrame(data)
    

    然后,您可以获得B更长的行,然后使用以下命令检索行:

    # Index where B is longest
    idx = df["B"].apply(len).idxmax()
    
    # Get that row
    df.iloc[idx, :]