简体   繁体   English

在Python中查找具有特定总数的列

[英]Find the column that has a certain total number in Python

If:如果:

sum(list(map(lambda x : sum(len(y) for y in x.split()), df['column2'].iloc[3])))

gets the total character in row number 3 of column number 2 in pandas dataframe df , then how to find the column that has a certain total number (for example: which index column has the total number of character of 43,382) ?获取pandas数据帧df中第2列的第3行的总字符数,那么如何找到具有特定总数的列(例如:哪个索引列的总字符数为43,382)?

EDITED : I tried this rather long code:编辑:我试过这个相当长的代码:

df.loc[df['column2'] == (sum(list(map(lambda x : sum(len(y) for y in x.split()), df['column2'])))).isin('43382')]

but I got error message:但我收到错误消息:

AttributeError: 'int' object has no attribute 'isin'

This is how my dataframe df looks like"这就是我的数据框df 的样子”

column1      column2                                            column3
amsterdam    school yeah right backtic escapes sport swimming   2016
rotterdam    nope yeah                                          2012
thehague     i now i can fly no you cannot swimming rope        2010
amsterdam    sport cycling in the winter makes me               2019

Can you try .isin(['43382']) .你能试试.isin(['43382'])

Hope this helps.希望这可以帮助。

尝试使用eq代替:

print(df[(sum(list(map(lambda x : sum(len(y) for y in x.split()), df['column2'])))) == ('43382')].dropna())

I figured it out by summing all the characters for each rows and return it to a new column = column4 :我通过对每行的所有字符求和并将其返回到一个新的 column = column4来解决这个问题

df['column4'] = df['column2'].apply(lambda x: sum(len(y) for y in x.split()))

and then simply just look at the desire character's total count with this code:然后只需使用以下代码查看所需角色的总数:

df[df['column4']== 43382]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM