[英]pandas - find rows if values in column of dtype list (object) has specific value
给定如下数据框
A B C-1 D BTP Type C1 Type C2
0 0 1 0 0 0 NaN [Type B]
1 0 2 1 1 14 [Type B] [Type B]
2 0 3 2 2 28 [Type A] [Type B]
3 0 4 3 3 42 [Type A, Type B] [Type A, Type B]
4 0 5 4 4 56 [Type A] [Type A, Type B]
想要为Type C1
列获取值为Type A
的行,为BTP
列获取值为42
的行,这应该返回行索引 3。
尝试了以下,但给出了错误KeyError: False
df.loc[(df['BTP'] == 42) & ('Type A' in df['Type C1'])]
我最终要做的是获取与上述条件匹配的行(这将是单行)并将列B
和C-1
的值提取为像{'B_val': 4, 'C_val': 3}
使用Series.str.join
加入Type C1
列中的列表,然后我们可以在该列上使用Series.str.contains
来检查给定的字符串,即Type A
是否存在于系列中,最后我们可以使用 boolean mask
过滤 dataframe 的行:
mask = df['BTP'].eq(42) & df['Type C1'].str.join('-').str.contains(r'\bType A\b')
df = df[mask]
结果:
# print(df)
A B C-1 D BTP Type C1 Type C2
3 0 4 3 3 42 [Type A, Type B] [Type A, Type B]
您可以使用
>>> type_a = df['Type C1'].apply(pd.Series).eq('Type A').any(1)
>>> df[df['BTP'].eq(42) & type_a]
A B C-1 D BTP Type C1 Type C2
3 0 4 3 3 42 [Type A, Type B] [Type A, Type B]
我使用自定义 function 解决了这个问题,根据考虑的列表是否包含“A 型”,返回每行的真/假值列表。
# Check if elem is present in column 'col'
def has_elem(col, elem):
result = []
for c in col:
if elem in c:
result.append(True)
else:
result.append(False)
return result
# Filter
df.loc[(df['BTP'] == 42) & has_elem(df['Type_C1'], 'Type A'), :]
您的代码不起作用的原因是因为 df['Type_C1'] 中的第二个过滤器子句'Type A' in df['Type_C1']
查找 object df['Type_C1']
系列中字符串'Type A'
的成员资格,因此返回False
. 相反,您需要为 dataframe 中的每一行返回一个真/假值序列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.