在Pandas Dataframe中灵活选择

时间:2017-07-16 20:59:54

标签: python pandas

我有以下几行代码:

import pandas as pd
df1 = pd.DataFrame({'Counterparty':['Bank','Client','GSE','PSE'],
                'Maturity':[2, 3, 2, 2],
                'Amount':[50, 55, 65, 55],
                'Match':[0,0,0,0]})

CounterpartyList=['Bank','Client']
MaturityList=[2,3]

df1.loc[(df1['Counterparty'].isin (CounterpartyList))& (df1['Maturity'].isin (MaturityList)),'Match']=420

如果两个列表中的任何一个(CounterpartyList或MaturityList)中都有'#',我希望代码的行为如下:

import pandas as pd
df1 = pd.DataFrame({'Counterparty':['Bank','Client','GSE','PSE'],
                'Maturity':[2, 3, 2, 2],
                'Amount':[50, 55, 65, 55],
                'Match':[0,0,0,0]})

CounterpartyList=['Bank','Client']  
MaturityList=['#']

df1.loc[(df1['Counterparty'].isin(CounterpartyList)) ,'Match']=420

即,当它们包含#时,我忽略匹配MaturityList或CounterpartyList的条件。

有关最有效方法的想法吗?我有很多条件,所以想要避免大的CASE条件

1 个答案:

答案 0 :(得分:2)

您可能想为每个列表创建一个bollean蒙版,然后将它们相交

@Override
public NameQualifierSupport getNameQualifierSupport() {
    return NameQualifierSupport.NONE;
}