繁体   English   中英

在数据帧列 - 熊猫中应用'或'条件

[英]Applying 'or' condition across dataframe columns- pandas

我想检查在任何dataframe行上给定数量的列是否具有任何一组值(不同列的不同集合)并相应地分配一个boolean - 我想我可能需要apply()any()但是没完全碰到它:

所以,对于数据帧:

bank_dict = {'Name' : ['A', 'B', 'C', 'D', 'E'],
        'Type' :     ['Retail', 'Corporate', 'Corporate', 'Wholesale', 'Retail'],
        'Overdraft': ['Y', 'Y', 'Y', 'N', 'N'],
        'Forex': ['USD', 'GBP', 'EUR', 'JPY', 'GBP']}

有真相清单:

truth_list = [bank_df['Type'].isin(['Retail']), bank_df['Overdraft'].isin(['Yes']), bank_df['Forex'].isin(['USD', 'GBP'])]

结果df应如下所示:

  Name       Type Overdraft Forex  TruthCol
0    A     Retail         Y   USD         1
1    B  Corporate         Y   GBP         1
2    C  Corporate         Y   EUR         1
3    D  Wholesale         N   JPY         0
4    E     Retail         N   GBP         1

谢谢,

我认为需要np.logical_or.reduce

bank_df['TruthCol'] = np.logical_or.reduce(truth_list).astype(int)
print (bank_df)
  Name       Type Overdraft Forex  TruthCol
0    A     Retail         Y   USD         1
1    B  Corporate         Y   GBP         1
2    C  Corporate         Y   EUR         1
3    D  Wholesale         N   JPY         0
4    E     Retail         N   GBP         1

另一种方法是将条件放在numpy.where中

bank_df['TruthCol'] = np.where(((bank_df['Type'] == 'Retail') | (bank_df['Overdraft'] == 'Y') | ((bank_df['Forex'] == 'USD') | (bank_df['Forex'] == 'GBP'))), 1, 0)

输出:

  Forex Name Overdraft       Type  TruthCol
0   USD    A         Y     Retail         1
1   GBP    B         Y  Corporate         1
2   EUR    C         Y  Corporate         1
3   JPY    D         N  Wholesale         0
4   GBP    E         N     Retail         1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM