3 回答
TA贡献1752条经验 获得超4个赞
首先,正确的语法是meds_df[['readcode_1', 'readcode_2','generic_name']](list索引切片中的列名)。这就是为什么你得到一个KeyError.
要回答您的问题,这是一种实现方法:
# Updated to use tuple per David's suggestion
idx = pd.concat((med_df[col].astype(str).str.startswith(tuple(list_to_extract)) for col in ['readcode_1', 'readcode_2','generic_name']), axis=1).any(axis=1)
med_df.loc[idx]
结果:
ID readcode_1 readcode_2 generic_name
1 1001 bxd1 1.146785e+09 Simvastatin
3 1003 NaN NaN Pravastatin
5 1005 bxd4 4.543234e+07 NaN
10 1010 bxde NaN NaN
TA贡献2012条经验 获得超12个赞
您可以通过这种方式进行申请:
list_to_extract = ["bxd", "Simvastatin", "1146785342", "45432344", "Pravastatin"]
bool_df = df[['readcode_1', 'readcode_2','generic_name']].apply(lambda x: x.str.startswith(tuple(list_to_extract), na=False), axis=1)
df.loc[bool_df[bool_df.any(axis=1)].index]
输出:
ID readcode_1 readcode_2 generic_name
1 1001 bxd1 1.146785e+09 Simvastatin
3 1003 NaN NaN Pravastatin
5 1005 bxd4 4.543234e+07 NaN
10 1010 bxde NaN NaN
感谢 r.ook 发现了一个小错误
TA贡献1776条经验 获得超12个赞
另一种解决方案,在重新创建数据帧之前,字符串处理发生在 vanilla python 中:
list_to_extract = ["bxd", "Simvastatin", "1146785342", "45432344", "Pravastatin"]
cols_to_search = ['readcode_1', 'readcode_2','generic_name']
output = [(ID, *searchbox)
for ID, searchbox in zip(df.ID,df.filter(cols_to_search).to_numpy())
if any([str(box).startswith(tuple(list_to_extract)) for box in searchbox])]
pd.DataFrame(output, columns = df.columns)
ID readcode_1 readcode_2 generic_name
0 1001 bxd1 1.146785e+09 Simvastatin
1 1003 NaN NaN Pravastatin
2 1005 bxd4 4.543234e+07 NaN
3 1010 bxde NaN NaN
添加回答
举报