简体   繁体   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

I have a dataframe which I need to filter rows out when the value returned by a dictionary using the value of the specific column is used as a dictionary key to return the keys value. 我有一个数据帧,当字典使用特定列的值返回的值被用作字典键来返回键值时,我需要过滤掉行。

I have been able to filter the rows based on the columns value but when i try to use the columns value in a dictionary in a similar fashion iit returns an error. 我已经能够根据列值过滤行,但是当我尝试以类似的方式使用字典中的列值时,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()

You could try replacing the column with the corresponding values from the dict. 您可以尝试使用dict中的相应值替换该列。 (You may need to use map instead of replace depending on how large the DataFrame is.) (根据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