簡體   English   中英

在Python中查找具有特定總數的列

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

如果:

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

獲取pandas數據幀df中第2列的第3行的總字符數,那么如何找到具有特定總數的列(例如:哪個索引列的總字符數為43,382)?

編輯:我試過這個相當長的代碼:

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

你能試試.isin(['43382'])

希望這可以幫助。

嘗試使用eq代替:

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

我通過對每行的所有字符求和並將其返回到一個新的 column = column4來解決這個問題

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

然后只需使用以下代碼查看所需角色的總數:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM