[英]Map multiple columns from pandas dataframe to a dictionary and conditionally return a value to a new column
I have a pandas dataframe with multiple columns and a dictionary with keys that correspond to the column names.我有一个 pandas dataframe 有多个列和一个字典,其中的键对应于列名。 I want to check the column values with respect to the dictionary values and return either a 'yes' or 'no' based on whether the column value meets a "greater than or equal to" condition.
我想根据字典值检查列值,并根据列值是否满足“大于或等于”条件返回“是”或“否”。
Example:例子:
import pandas as pd
dfdict = {'col1': [1,2,3], 'col2':[2,3,4], 'col3': [3.2, 4.2, 7.7]}
checkdict = {'col1': 2, 'col2': 3, 'col3': 1.5}
df = pd.DataFrame(dfdict)
For each column, for each row, check whether the row value is greater than or equal to than the value in the dictionary.对于每一列,对于每一行,检查行值是否大于或等于字典中的值。 For that row, if any of the columns meet the condition, return a "yes" to a newly created column, else return a "no".
对于该行,如果任何列满足条件,则对新创建的列返回“是”,否则返回“否”。
What I've tried:我试过的:
def checkcond(element):
if not math.isnan(element):
x = checkdict[element]
return 1 if element >= x else 0
else:
pass
df['test'] = df.applymap(checkcond)
but of course this doesn't work because the row value is supplied to the checkcond function rather than the column name and row.但当然这不起作用,因为行值提供给 checkcond function 而不是列名和行。
I also tried:我也试过:
df['test'] = pd.np.where(df[['col1', 'col2', 'col3']].ge(0).any(1, skipna=True), 'Y', 'N')
But that will only take one value for the "ge" condition, whereas I want to check the row value with respect to the dictionary value for each of the columns.但这只会为“ge”条件取一个值,而我想根据每一列的字典值检查行值。
Any suggestions would be appreciated!任何建议,将不胜感激!
Convert your dictionary to Series and perform a simple comparison:将您的字典转换为 Series 并执行简单的比较:
df.ge(pd.Series(checkdict)).replace({True: 'yes', False: 'no'})
output: output:
col1 col2 col3
0 no no yes
1 no no yes
2 yes yes yes
To get aggregation per row:要获得每行的聚合:
df['any'] = df.ge(pd.Series(checkdict)).any(1).map({True: 'yes', False: 'no'})
output: output:
col1 col2 col3 any
0 1 2 3.2 yes
1 2 3 4.2 yes
2 3 4 7.7 yes
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.