简体   繁体   English

pandas - 如果 dtype 列表(对象)的列中的值具有特定值,则查找行

[英]pandas - find rows if values in column of dtype list (object) has specific value

Given a data frame like below给定如下数据框

   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]

want to fetch rows with value Type A for column Type C1 and 42 for column BTP which should return row index 3.想要为Type C1列获取值为Type A的行,为BTP列获取值为42的行,这应该返回行索引 3。

Tried the following, but gives an error KeyError: False尝试了以下,但给出了错误KeyError: False

df.loc[(df['BTP'] == 42) & ('Type A' in df['Type C1'])]

What I'm ultimately trying to do is to fetch row that will match the above condition (which would be a single row) and extract the values for columns B and C-1 as a dict like {'B_val': 4, 'C_val': 3}我最终要做的是获取与上述条件匹配的行(这将是单行)并将列BC-1的值提取为像{'B_val': 4, 'C_val': 3}

Use, Series.str.join to join the lists in column Type C1 , then we could be able to use Series.str.contains on this column to check whether the given string ie Type A is present in the series or not, finally we can filter the rows of dataframe using the boolean mask :使用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]

Result:结果:

# 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]

You can use您可以使用

>>> 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]

I solved this with a custom function to return a list of True/False values for each row, based on whether the list under consideration contains 'Type A' or not.我使用自定义 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'), :]

The reason your code doesn't work is because the 2nd filter clause 'Type A' in df['Type_C1'] looks for membership of the string 'Type A' in the Series object df['Type_C1'] , and consequently returns False .您的代码不起作用的原因是因为 df['Type_C1'] 中的第二个过滤器子句'Type A' in df['Type_C1']查找 object df['Type_C1']系列中字符串'Type A'的成员资格,因此返回False . Instead, you need to return a sequence of True/False values, 1 for each row in your dataframe.相反,您需要为 dataframe 中的每一行返回一个真/假值序列。

暂无
暂无

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

相关问题 给定值列表的大熊猫在列中查找具有此值的行 - pandas, given list of values, find rows that has this value at column Pandas - 通过指定分隔符将数据类型为 object(字符串)的列拆分为数据类型列表 - Pandas - split column with dtype object (string) to dtype list by specifying delimiter 当列在python pandas中具有对象dtype时,如何基于“列A”中的值填充“列B”? - How to fill “column B” based on value in “column A” when the column has object dtype in python pandas? 如果 pandas df 列具有特定值,则另一列仅允许值列表 - If a pandas df column has a specific value, another column only allow a list of values 在 pandas 中拆分 object dtype 列 - Splitting an object dtype column in pandas 将 3*2 数组 (dtype=object) 的第二列值存储在列表中 - store seconde column's values of a 3*2 array (dtype=object) in a list Pandas - 查找具有特定值的所有行并保留具有匹配列值的所有行 - Pandas - Find all rows with a specific value and keep all rows with matching column value 在 Pandas 中添加具有特定 dtype 的新列 - Adding a new column with specific dtype in pandas 从混合 dtype 列中,从 python pandas 中的特定列值中提取字符串 - From a mixed dtype column, extract string from specific column values in python pandas 在 datafrane Pandas 中将 Object dtype 列转换为 Number Dtype - Convert an Object dtype column to Number Dtype in a datafrane Pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM