Python 在 Pandas 数据框中使用 len()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39306229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 22:07:02  来源:igfitidea点击:

using len() in Pandas dataframe

pythonpandasdataframe

提问by Dong

This is the look of my DataFrame:

这是我的样子DataFrame

   StateAb    GivenNm    Surname                  PartyNm PartyAb  ElectedOrder
35      WA        Joe    BULLOCK   Australian Labor Party     ALP             2
36      WA  Michaelia       CASH                  Liberal      LP             3
37      WA      Linda   REYNOLDS                  Liberal      LP             4
38      WA      Wayne  DROPULICH  Australian Sports Party    SPRT             5
39      WA      Scott     LUDLAM          The Greens (WA)     GRN             6

and I want to list a list of senators whose surname is more than 9 characters long.

我想列出一个姓氏超过 9 个字符的参议员名单。

So I think the code should be like this:

所以我觉得代码应该是这样的:

df[len(df.Surname) > 9]

but this raises a KeyError, where did I go wrong?

但这引发了一个KeyError,我哪里出错了?

回答by ayhan

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

根据列中字符串的长度过滤 DataFrame 的正确方法是

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).

df['Surname'].str.len()为 surname 列创建一系列长度并df[df['Surname'].str.len() > 9]过滤掉小于或等于 9 的长度。您所做的是检查 Series 本身的长度(它有多少行)。

回答by Sytse Reitsma

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

看看python过滤器功能。它完全符合您的要求。

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

>>[{'Surname': 'Bullock-ish'}]

Sytse

系统