using len() in Pandas dataframe

前端 未结 2 2071
忘掉有多难
忘掉有多难 2020-12-10 16:02

This is the look of my DataFrame:

   StateAb    GivenNm    Surname                  PartyNm PartyAb  ElectedOrder
35      WA        Joe    BULLOCK            


        
相关标签:
2条回答
  • 2020-12-10 16:31

    The correct way to filter a DataFrame based on the length of strings in a column is

    df[df['Surname'].str.len() > 9]
    

    df['Surname'].str.len() creates a Series of lengths for the surname column and df[df['Surname'].str.len() > 9] filters out the ones less than or equal to 9. What you did is to check the length of the Series itself (how many rows it has).

    0 讨论(0)
  • 2020-12-10 16:33

    Have a look at the python filter function. It does exactly what you want.

    df = [
        {"Surname": "Bullock-ish"},
        {"Surname": "Cash"},
        {"Surname": "Reynolds"},
    ]
    longnames = list(filter(lambda s: len(s["Surname"]) > 9, df))
    print(longnames)
    
    >>[{'Surname': 'Bullock-ish'}]
    

    Sytse

    0 讨论(0)
提交回复
热议问题