简体   繁体   English

如何过滤包含 Python dataframe 中的元素列表的列

[英]How to filter a column with list of elements in Python dataframe

I have a data frame like this:我有一个这样的数据框:

df
ID     Col_1
1      Apple, Cherry, Banana
2      Apple, Mango
3      Kiwi, Cherry
4      Apple, Cherry, Pear
5      Apple, Melon
6      Papaya, Cherry

I want to filter the data frame in these 3 ways:我想以这三种方式过滤数据框:

  1. Col_1 has both Apple & Cherry Col_1 同时拥有 Apple 和 Cherry
  2. Col_1 has Apple but not Cherry Col_1 有 Apple 但没有 Cherry
  3. Col_1 has Cherry but not Apple Col_1 有 Cherry 但没有 Apple

This is how my output looks like:这就是我的 output 的样子:

1. Col_1 has both Apple & Cherry   

Output:
ID     Col_1
1      Apple, Cherry, Banana
4      Apple, Cherry, Pear


2. Col_1 has Apple but not Cherry

Output:
ID     Col_1
2      Apple, Mango
5      Apple, Melon

3. Col_1 has Cherry but not Apple

Output:
ID     Col_1
3      Kiwi, Cherry
6      Papaya, Cherry

Can anyone help me with this?谁能帮我这个?

Let's first start by creating OP's dataframe我们首先从创建 OP 的 dataframe 开始

df = pd.DataFrame({'ID': [1, 2, 3, 4, 5, 6],
                        'Col_1': ['Apple, Cherry, Banana', 'Apple, Mango', 'Kiwi, Cherry', 'Apple, Cherry, Pear', 'Apple, Melon', 'Papaya, Cherry']})

[Out]:
   ID                  Col_1
0   1  Apple, Cherry, Banana
1   2           Apple, Mango
2   3           Kiwi, Cherry
3   4    Apple, Cherry, Pear
4   5           Apple, Melon
5   6         Papaya, Cherry

Based on what OP shared, considering that the constraints are always dependent on apple and cherry , one can create a function, let's call it filter_df , that takes as input a dataframe and two strings as follows根据 OP 共享的内容,考虑到约束始终依赖于applecherry ,可以创建一个 function,我们称之为filter_df ,它将 dataframe 和两个字符串作为输入,如下所示

def filter_df(df, s1, s2):

    # Col_1 has both Apple & Cherry
    df1 = df[df['Col_1'].str.contains(s1) & df['Col_1'].str.contains(s2)]

    # Col_1 has Apple but not Cherry
    df2 = df[df['Col_1'].str.contains(s1) & ~df['Col_1'].str.contains(s2)]

    # Col_1 has Cherry but not Apple
    df3 = df[df['Col_1'].str.contains(s2) & ~df['Col_1'].str.contains(s1)]

    return df1, df2, df3

Then, if one applies the function filter_df to the dataframe df , with the strings Apple and Cherry , one gets the following results然后,如果将 function filter_df应用于 dataframe df ,并使用字符串AppleCherry ,则会得到以下结果

df1, df2, df3 = filter_df(df, 'Apple', 'Cherry')

# df1 - Col_1 has both Apple & Cherry

[Out]:
   ID                  Col_1
0   1  Apple, Cherry, Banana
3   4    Apple, Cherry, Pear


# df2 - Col_1 has Apple but not Cherry

[Out]:
   ID         Col_1
1   2  Apple, Mango
4   5  Apple, Melon


# df3 - Col_1 has Cherry but not Apple

[Out]:
   ID           Col_1
2   3    Kiwi, Cherry
5   6  Papaya, Cherry

If one wants to change the strings to consider, for example, Kiwi and Mango , or other strings, one can do that as well.如果想要更改要考虑的字符串,例如KiwiMango或其他字符串,也可以这样做。 Also, if the conditions change in the future, one can easily adjust the function filter_df accordingly.此外,如果将来条件发生变化,可以相应地轻松调整 function filter_df

暂无
暂无

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

相关问题 如何过滤 dataframe 中的列表列? - How to filter a list column in a dataframe? 如何根据Python中列的行中列表中的值过滤数据帧? - How to filter a dataframe based on the values present in the list in the rows of a column in Python? 如何从列中选择所有元素,它是python中DataFrame中的列表? - How to select all elements from column that it is a list in DataFrame in python? 如何将 dataframe 列中的多个句子组合到 Python 中的单个元素列表中 - How to combine Multiple sentences in a dataframe column to a single list of elements in Python 如何过滤 pandas dataframe 到 select 列列表 - How to filter pandas dataframe to select list of column 如何在数据框列中过滤数字字符串列表? - How to filter numeric string list in dataframe column? I want to count the elements of a python list that is within a dataframe, and for the output to be a column in the dataframe. 我怎么做? - I want to count the elements of a python list that is within a dataframe, and for the output to be a column in the dataframe. How do I do that? 如何在Python中以整数开头过滤数据框中的列? - How to filter a column in dataframe starting with integers in Python? Python将列添加到Pandas Dataframe,这是另一列中的列表元素计数 - Python Add Column to Pandas Dataframe That is a Count of List Elements in Another Column 如何将列表与数据帧列进行比较,如果元素列与列表之间的元素匹配,则从列表中删除元素? - How to compare list against column of dataframe, and remove elements from list if elements match between dataframe column and list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM