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

2024-04-19 16:46:54 发布

您现在位置:Python中文网/ 问答频道 /正文

如果:

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

获取数据帧中第2列第3行的总字符df,然后如何查找具有特定总字符数的列(例如:哪个索引列的总字符数为43382)

已编辑: 我尝试了这个相当长的代码:

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

但我收到了错误信息:

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

这就是我的数据帧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

Tags: lambdanoinmapdfforlen字符
3条回答

我通过对每行的所有字符求和并将其返回到一个新列=column4,计算出了它:

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

然后简单地用下面的代码查看欲望角色的总计数:

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

你能试试吗

希望这有帮助

请尝试改用eq

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

相关问题 更多 >