在 pandas 中按多个条件选择数据框行
Selecting dataframe rows by muliple conditions in pandas
根据某些条件,我可以从 pandas df select 行:
cardio = df[df.indications == 'Cardiovascular / cardiology']
end_aug = '2020-08-31'
start_aug = '2020-08-01'
mask = (df['date']>start_aug) & (df['date']<=end_aug)
df = df.loc[mask,df['indications']]
但我还想使用“cardio”变量来缩小 ['indications'] 列中的 selection 范围,例如:
df = df.loc[mask,df['indications']== 'Neoplasms / cancer / oncology']
但是上面的代码返回了一个错误:IndexingError: Unalignable boolean Series provided as indexer (boolean Series and of the indexed object do not match).
如何使用面罩和另一个 selection,在我的例子中是使用“cardio”变量?
另一个解决方案:
searchfor = ['Neoplasms', 'cancer', 'oncology']
df = df[df['indications'].str.contains('|'.join(searchfor))]
根据某些条件,我可以从 pandas df select 行:
cardio = df[df.indications == 'Cardiovascular / cardiology']
end_aug = '2020-08-31'
start_aug = '2020-08-01'
mask = (df['date']>start_aug) & (df['date']<=end_aug)
df = df.loc[mask,df['indications']]
但我还想使用“cardio”变量来缩小 ['indications'] 列中的 selection 范围,例如:
df = df.loc[mask,df['indications']== 'Neoplasms / cancer / oncology']
但是上面的代码返回了一个错误:IndexingError: Unalignable boolean Series provided as indexer (boolean Series and of the indexed object do not match).
如何使用面罩和另一个 selection,在我的例子中是使用“cardio”变量?
另一个解决方案:
searchfor = ['Neoplasms', 'cancer', 'oncology']
df = df[df['indications'].str.contains('|'.join(searchfor))]