簡體   English   中英

如何使用columns值作為字典的鍵,根據返回的值過濾pandas df的行

[英]How to filter rows of a pandas df using the columns value as the key to a dictionary based on its returned value

我有一個數據幀,當字典使用特定列的值返回的值被用作字典鍵來返回鍵值時,我需要過濾掉行。

我已經能夠根據列值過濾行,但是當我嘗試以類似的方式使用字典中的列值時,iit會返回錯誤。

TypeError: 'Series' objects are mutable, thus they cannot be hashed
indexNames = df[ accounting_type_dict[df['fund_id']] == 'ETFs' ].index
df.drop(indexNames , inplace=True)

使用map然后過濾

newdf=df[df['fund_id'].map(accounting_type_dict) == 'ETFs'].copy()

您可以嘗試使用dict中的相應值替換該列。 (根據DataFrame的大小,您可能需要使用map而不是replace。)

indexNames = df[ df['fund_id'].replace(accounting_type_dict) == 'ETFs' ].index
df.drop(indexNames , inplace=True)

暫無
暫無

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

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