在Pandas数据帧中使用len()

时间:2016-09-03 11:09:13

标签: python pandas dataframe

这是我的- (void)fetchAllUserAndStopWithCompletion:(void(^)(id resultResponse, BOOL isComplete))completion { // Step 1: Define default parameters and limits. __block NSInteger totalNumberOfUser = 4; __block NSInteger currentRequest = 0; __block dispatch_block_t t_request; dispatch_block_t request = [^{ // Step 2: Fetch User information for currentRequest id. [UserRequest fetchUserRequestWithId:<UserId or Any Unique Id> successBlock:^(id resultResponse, NSError *error) { currentRequest += 1; // Step 3: Check for remaining request. If request is remaining then call ’t_request()’. if (currentRequest < totalNumberOfUser) { // Request Remaining if (completion) { id response = (!error)?resultResponse:nil; completion(response, NO); } t_request(); } else { // All Request Completed if (completion) { id response = (!error)?resultResponse:nil; completion(response, YES); } } }]; } copy]; t_request = request; request(); }

的外观
   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

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

所以我认为代码应该是这样的:

DataFrame

但这会引发df[len(df.Surname) > 9] ,我哪里出错?

2 个答案:

答案 0 :(得分:16)

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

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

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

答案 1 :(得分:0)

看一下python filter函数。它完全符合您的要求。

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

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

Sytse