如何在数据框架中跨成对行并删除熊猫中的非交流元素

发布于 2025-01-23 11:13:57 字数 5115 浏览 0 评论 0原文

我有此数据框:

import pandas as pd
data = {'small group': [['a1', 'a2'], ['a2', 'a3'],['a3','a4'], ['d1', 'd2'], ['d2', 'd3'], ['d3', 'd4'], ['c1', 'c2'], ['c2', 'c3'], ['f1', 'f2']],
        'all_groups': [[['a1', 'a2'], ['a2', 'a3'],['a3','a4']], [['a1', 'a2'], ['a2', 'a3'],['a3','a4']],
                       [['a1', 'a2'], ['a2', 'a3'],['a3','a4']],
                   [['d1', 'd2'], ['d2', 'd3'], ['d3', 'd4']], [['d1', 'd2'], ['d2', 'd3'],
                    ['d3', 'd4']], [['d1', 'd2'], ['d2', 'd3'], ['d3', 'd4']], [['c1', 'c2'], ['c2', 'c3']],
                   [['c1', 'c2'], ['c2', 'c3']], [['f1', 'f2']]],
        'name':[['Alina','Kate'],['Alina','Kate','Diana'],['Kate','Diana'],['Mike','Bob'],['Ian','Lili'],['George','Cloud','Ian','Petro'],['Jone','Petro','Lili'],['Marinet','Yu','Chloe','Ian'],['Nate','Rose']],
        'day': [28, 28,28, 18, 18, 18, 20, 20, 3],

        }
df = pd.DataFrame(data)

输出:

+----+---------------+--------------------------------------------+-------------------------------------+-------+
|    | small group   | all_groups                                 | name                                |   day |
|----+---------------+--------------------------------------------+-------------------------------------+-------|
|  0 | ['a1', 'a2']  | [['a1', 'a2'], ['a2', 'a3'], ['a3', 'a4']] | ['Alina', 'Kate']                   |    28 |
|  1 | ['a2', 'a3']  | [['a1', 'a2'], ['a2', 'a3'], ['a3', 'a4']] | ['Alina', 'Kate', 'Diana']          |    28 |
|  2 | ['a3', 'a4']  | [['a1', 'a2'], ['a2', 'a3'], ['a3', 'a4']] | ['Kate', 'Diana']                   |    28 |
|  3 | ['d1', 'd2']  | [['d1', 'd2'], ['d2', 'd3'], ['d3', 'd4']] | ['Mike', 'Bob']                     |    18 |
|  4 | ['d2', 'd3']  | [['d1', 'd2'], ['d2', 'd3'], ['d3', 'd4']] | ['Ian', 'Lili']                     |    18 |
|  5 | ['d3', 'd4']  | [['d1', 'd2'], ['d2', 'd3'], ['d3', 'd4']] | ['George', 'Cloud', 'Ian', 'Petro'] |    18 |
|  6 | ['c1', 'c2']  | [['c1', 'c2'], ['c2', 'c3']]               | ['Jone', 'Petro', 'Lili']           |    20 |
|  7 | ['c2', 'c3']  | [['c1', 'c2'], ['c2', 'c3']]               | ['Marinet', 'Yu', 'Chloe', 'Ian']   |    20 |
|  8 | ['f1', 'f2']  | [['f1', 'f2']]                             | ['Nate', 'Rose']                    |     3 |
+----+---------------+--------------------------------------------+-------------------------------------+-------+

我需要在分组时将列与成对的名称相交。我需要与所有可能的组合相交,而不仅仅是按顺序结合。我了解如何为所有列表进行此操作,但我不明白如何成对输出。我想使用一个称为相交和聚合的函数。但是也许还有另一种方式

df_new = (df.groupby(['day','all_groups'])
        .agg({'name': intersect})).reset_index()

+----+-------+------------------------------+-------------------+--------------------------------------------+
|    |   day |  pair                        | name_intersect    | all_groups                                 |
|----+-------+------------------------------+-------------------+--------------------------------------------|
|  0 |    28 | [['a1', 'a2'], ['a2', 'a3']] | ['Alina', 'Kate'] | [['a1', 'a2'], ['a2', 'a3'], ['a3', 'a4']] |
|  1 |    28 | [['a1', 'a2'], ['a3', 'a4']] | ['Kate']          | [['a1', 'a2'], ['a2', 'a3'], ['a3', 'a4']] |
|  2 |    28 | [['a2', 'a3'], ['a3', 'a4']] | ['Kate', 'Diana'] | [['a1', 'a2'], ['a2', 'a3'], ['a3', 'a4']] |
|  3 |    18 | [['d1', 'd2'], ['d2', 'd3']] | []                | [['d1', 'd2'], ['d2', 'd3'], ['d3', 'd4']] |
|  4 |    18 | [['d1', 'd2'], ['d3', 'd4']] | []                | [['d1', 'd2'], ['d2', 'd3'], ['d3', 'd4']] |
|  5 |    18 | [['d2', 'd3'], ['d3', 'd4']] | ['Ian']           | [['d1', 'd2'], ['d2', 'd3'], ['d3', 'd4']] |
|  6 |    20 | [['c1', 'c2'], ['c2', 'c3']] | []                | [['c1', 'c2'], ['c2', 'c3']]               |
|  7 |     3 | ['f1', 'f2']                 | []                | [['f1', 'f2']]                             |
+----+-------+------------------------------+-------------------+--------------------------------------------+

,然后我想在name_intersect列中删除空行。并考虑对['d1','d2']等案例 [['d1','d2'],['d2','d3'], ['d3','d4']] ,因为使用此对,没有与其他人的交集(我想从此列表中删除这对)。因此,我想获得这样的输出:

+----+-------+------------------------------+-------------------+--------------------------------------------+
|    |   day |  pair                        | name_intersect    | all_groups                                 |
|----+-------+------------------------------+-------------------+--------------------------------------------|
|  0 |    28 | [['a1', 'a2'], ['a2', 'a3']] | ['Alina', 'Kate'] | [['a1', 'a2'], ['a2', 'a3'], ['a3', 'a4']] |
|  1 |    28 | [['a1', 'a2'], ['a3', 'a4']] | ['Kate']          | [['a1', 'a2'], ['a2', 'a3'], ['a3', 'a4']] |
|  2 |    28 | [['a2', 'a3'], ['a3', 'a4']] | ['Kate', 'Diana'] | [['a1', 'a2'], ['a2', 'a3'], ['a3', 'a4']] |
|  3 |    18 | [['d2', 'd3'], ['d3', 'd4']] | ['Ian']           | [['d2', 'd3'], ['d3', 'd4']]               |
+----+-------+------------------------------+-------------------+--------------------------------------------+

I have this dataframe:

import pandas as pd
data = {'small group': [['a1', 'a2'], ['a2', 'a3'],['a3','a4'], ['d1', 'd2'], ['d2', 'd3'], ['d3', 'd4'], ['c1', 'c2'], ['c2', 'c3'], ['f1', 'f2']],
        'all_groups': [[['a1', 'a2'], ['a2', 'a3'],['a3','a4']], [['a1', 'a2'], ['a2', 'a3'],['a3','a4']],
                       [['a1', 'a2'], ['a2', 'a3'],['a3','a4']],
                   [['d1', 'd2'], ['d2', 'd3'], ['d3', 'd4']], [['d1', 'd2'], ['d2', 'd3'],
                    ['d3', 'd4']], [['d1', 'd2'], ['d2', 'd3'], ['d3', 'd4']], [['c1', 'c2'], ['c2', 'c3']],
                   [['c1', 'c2'], ['c2', 'c3']], [['f1', 'f2']]],
        'name':[['Alina','Kate'],['Alina','Kate','Diana'],['Kate','Diana'],['Mike','Bob'],['Ian','Lili'],['George','Cloud','Ian','Petro'],['Jone','Petro','Lili'],['Marinet','Yu','Chloe','Ian'],['Nate','Rose']],
        'day': [28, 28,28, 18, 18, 18, 20, 20, 3],

        }
df = pd.DataFrame(data)

Output:

+----+---------------+--------------------------------------------+-------------------------------------+-------+
|    | small group   | all_groups                                 | name                                |   day |
|----+---------------+--------------------------------------------+-------------------------------------+-------|
|  0 | ['a1', 'a2']  | [['a1', 'a2'], ['a2', 'a3'], ['a3', 'a4']] | ['Alina', 'Kate']                   |    28 |
|  1 | ['a2', 'a3']  | [['a1', 'a2'], ['a2', 'a3'], ['a3', 'a4']] | ['Alina', 'Kate', 'Diana']          |    28 |
|  2 | ['a3', 'a4']  | [['a1', 'a2'], ['a2', 'a3'], ['a3', 'a4']] | ['Kate', 'Diana']                   |    28 |
|  3 | ['d1', 'd2']  | [['d1', 'd2'], ['d2', 'd3'], ['d3', 'd4']] | ['Mike', 'Bob']                     |    18 |
|  4 | ['d2', 'd3']  | [['d1', 'd2'], ['d2', 'd3'], ['d3', 'd4']] | ['Ian', 'Lili']                     |    18 |
|  5 | ['d3', 'd4']  | [['d1', 'd2'], ['d2', 'd3'], ['d3', 'd4']] | ['George', 'Cloud', 'Ian', 'Petro'] |    18 |
|  6 | ['c1', 'c2']  | [['c1', 'c2'], ['c2', 'c3']]               | ['Jone', 'Petro', 'Lili']           |    20 |
|  7 | ['c2', 'c3']  | [['c1', 'c2'], ['c2', 'c3']]               | ['Marinet', 'Yu', 'Chloe', 'Ian']   |    20 |
|  8 | ['f1', 'f2']  | [['f1', 'f2']]                             | ['Nate', 'Rose']                    |     3 |
+----+---------------+--------------------------------------------+-------------------------------------+-------+

I need to intersect a column with names in pairs when I groupby.I need to intersect all possible combinations, not just in order. I understand how to do this for all lists, but I don’t understand how to output in pairs.I would like to use a function called intersect and aggregate. But maybe there is another way

df_new = (df.groupby(['day','all_groups'])
        .agg({'name': intersect})).reset_index()

+----+-------+------------------------------+-------------------+--------------------------------------------+
|    |   day |  pair                        | name_intersect    | all_groups                                 |
|----+-------+------------------------------+-------------------+--------------------------------------------|
|  0 |    28 | [['a1', 'a2'], ['a2', 'a3']] | ['Alina', 'Kate'] | [['a1', 'a2'], ['a2', 'a3'], ['a3', 'a4']] |
|  1 |    28 | [['a1', 'a2'], ['a3', 'a4']] | ['Kate']          | [['a1', 'a2'], ['a2', 'a3'], ['a3', 'a4']] |
|  2 |    28 | [['a2', 'a3'], ['a3', 'a4']] | ['Kate', 'Diana'] | [['a1', 'a2'], ['a2', 'a3'], ['a3', 'a4']] |
|  3 |    18 | [['d1', 'd2'], ['d2', 'd3']] | []                | [['d1', 'd2'], ['d2', 'd3'], ['d3', 'd4']] |
|  4 |    18 | [['d1', 'd2'], ['d3', 'd4']] | []                | [['d1', 'd2'], ['d2', 'd3'], ['d3', 'd4']] |
|  5 |    18 | [['d2', 'd3'], ['d3', 'd4']] | ['Ian']           | [['d1', 'd2'], ['d2', 'd3'], ['d3', 'd4']] |
|  6 |    20 | [['c1', 'c2'], ['c2', 'c3']] | []                | [['c1', 'c2'], ['c2', 'c3']]               |
|  7 |     3 | ['f1', 'f2']                 | []                | [['f1', 'f2']]                             |
+----+-------+------------------------------+-------------------+--------------------------------------------+

Then I'd like to remove empty rows in the name_intersect column. And consider such cases as pair of ['d1', 'd2'] in the list [['d1', 'd2'], ['d2', 'd3'], ['d3', 'd4']], because with this pair there are no intersections with others(I want to remove this pair from this list). So, I'd like to get an output like this:

+----+-------+------------------------------+-------------------+--------------------------------------------+
|    |   day |  pair                        | name_intersect    | all_groups                                 |
|----+-------+------------------------------+-------------------+--------------------------------------------|
|  0 |    28 | [['a1', 'a2'], ['a2', 'a3']] | ['Alina', 'Kate'] | [['a1', 'a2'], ['a2', 'a3'], ['a3', 'a4']] |
|  1 |    28 | [['a1', 'a2'], ['a3', 'a4']] | ['Kate']          | [['a1', 'a2'], ['a2', 'a3'], ['a3', 'a4']] |
|  2 |    28 | [['a2', 'a3'], ['a3', 'a4']] | ['Kate', 'Diana'] | [['a1', 'a2'], ['a2', 'a3'], ['a3', 'a4']] |
|  3 |    18 | [['d2', 'd3'], ['d3', 'd4']] | ['Ian']           | [['d2', 'd3'], ['d3', 'd4']]               |
+----+-------+------------------------------+-------------------+--------------------------------------------+

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌上芳菲 2025-01-30 11:13:57
import pandas as pd
data = {'small group': [['a1', 'a2'], ['a2', 'a3'],['a3','a4'], ['d1', 'd2'], ['d2', 'd3'], ['d3', 'd4'], ['c1', 'c2'], ['c2', 'c3'], ['f1', 'f2']],
        'all_groups': [[['a1', 'a2'], ['a2', 'a3'],['a3','a4']], [['a1', 'a2'], ['a2', 'a3'],['a3','a4']],
                       [['a1', 'a2'], ['a2', 'a3'],['a3','a4']],
                   [['d1', 'd2'], ['d2', 'd3'], ['d3', 'd4']], [['d1', 'd2'], ['d2', 'd3'],
                    ['d3', 'd4']], [['d1', 'd2'], ['d2', 'd3'], ['d3', 'd4']], [['c1', 'c2'], ['c2', 'c3']],
                   [['c1', 'c2'], ['c2', 'c3']], [['f1', 'f2']]],
        'name':[['Alina','Kate'],['Alina','Kate','Diana'],['Kate','Diana'],['Mike','Bob'],['Ian','Lili'],['George','Cloud','Ian','Petro'],['Jone','Petro','Lili'],['Marinet','Yu','Chloe','Ian'],['Nate','Rose']],
        'day': [28, 28,28, 18, 18, 18, 20, 20, 3],

        }
df = pd.DataFrame(data)
newdf = df.shift(1).fillna('[]').reset_index().rename(columns={'name':'newcol'})

b=newdf.newcol.tolist()
a=df.name.tolist()


c = [[x for x in ii if  x in jj] for ii, jj in zip(a, b)]
df['name_intersect']=c


Out:
    small group                      all_groups  ... day  name_intersect
0    [a1, a2]  [[a1, a2], [a2, a3], [a3, a4]]  ...  28              []
1    [a2, a3]  [[a1, a2], [a2, a3], [a3, a4]]  ...  28   [Alina, Kate]
2    [a3, a4]  [[a1, a2], [a2, a3], [a3, a4]]  ...  28   [Kate, Diana]
3    [d1, d2]  [[d1, d2], [d2, d3], [d3, d4]]  ...  18              []
4    [d2, d3]  [[d1, d2], [d2, d3], [d3, d4]]  ...  18              []
5    [d3, d4]  [[d1, d2], [d2, d3], [d3, d4]]  ...  18           [Ian]
6    [c1, c2]            [[c1, c2], [c2, c3]]  ...  20         [Petro]
7    [c2, c3]            [[c1, c2], [c2, c3]]  ...  20              []
8    [f1, f2]                      [[f1, f2]]  ...   3              []

如果要将name_intersect用-1移动列,则

df['name_intersect']=df['name_intersect'].shift(-1).fillna('[]')

Out: 
    

small group                      all_groups                         name  day name_intersect
0    [a1, a2]  [[a1, a2], [a2, a3], [a3, a4]]                [Alina, Kate]   28  [Alina, Kate]
1    [a2, a3]  [[a1, a2], [a2, a3], [a3, a4]]         [Alina, Kate, Diana]   28  [Kate, Diana]
2    [a3, a4]  [[a1, a2], [a2, a3], [a3, a4]]                [Kate, Diana]   28             []
3    [d1, d2]  [[d1, d2], [d2, d3], [d3, d4]]                  [Mike, Bob]   18             []
4    [d2, d3]  [[d1, d2], [d2, d3], [d3, d4]]                  [Ian, Lili]   18          [Ian]
5    [d3, d4]  [[d1, d2], [d2, d3], [d3, d4]]  [George, Cloud, Ian, Petro]   18        [Petro]
6    [c1, c2]            [[c1, c2], [c2, c3]]          [Jone, Petro, Lili]   20             []
7    [c2, c3]            [[c1, c2], [c2, c3]]    [Marinet, Yu, Chloe, Ian]   20             []
8    [f1, f2]                      [[f1, f2]]                 [Nate, Rose]    3             []

现在,删除包含空列表的行> name_intersect 并获取df_final

df_final=df[df.astype(str)['name_intersect'] != '[]']

out:
         small group                      all_groups                         name  day name_intersect
0    [a1, a2]  [[a1, a2], [a2, a3], [a3, a4]]                [Alina, Kate]   28  [Alina, Kate]
1    [a2, a3]  [[a1, a2], [a2, a3], [a3, a4]]         [Alina, Kate, Diana]   28  [Kate, Diana]
4    [d2, d3]  [[d1, d2], [d2, d3], [d3, d4]]                  [Ian, Lili]   18          [Ian]
5    [d3, d4]  [[d1, d2], [d2, d3], [d3, d4]]  [George, Cloud, Ian, Petro]   18        [Petro]

整个代码:

df = pd.DataFrame(data)
newdf = df.shift(1).fillna('[]').reset_index().rename(columns={'name':'newcol'})

b=newdf.newcol.tolist()
a=df.name.tolist()


c = [[x for x in ii if  x in jj] for ii, jj in zip(a, b)]
df['name_intersect']=c

df['name_intersect']=df['name_intersect'].shift(-1).fillna('[]')

df_final=df[df.astype(str)['name_intersect'] != '[]']
import pandas as pd
data = {'small group': [['a1', 'a2'], ['a2', 'a3'],['a3','a4'], ['d1', 'd2'], ['d2', 'd3'], ['d3', 'd4'], ['c1', 'c2'], ['c2', 'c3'], ['f1', 'f2']],
        'all_groups': [[['a1', 'a2'], ['a2', 'a3'],['a3','a4']], [['a1', 'a2'], ['a2', 'a3'],['a3','a4']],
                       [['a1', 'a2'], ['a2', 'a3'],['a3','a4']],
                   [['d1', 'd2'], ['d2', 'd3'], ['d3', 'd4']], [['d1', 'd2'], ['d2', 'd3'],
                    ['d3', 'd4']], [['d1', 'd2'], ['d2', 'd3'], ['d3', 'd4']], [['c1', 'c2'], ['c2', 'c3']],
                   [['c1', 'c2'], ['c2', 'c3']], [['f1', 'f2']]],
        'name':[['Alina','Kate'],['Alina','Kate','Diana'],['Kate','Diana'],['Mike','Bob'],['Ian','Lili'],['George','Cloud','Ian','Petro'],['Jone','Petro','Lili'],['Marinet','Yu','Chloe','Ian'],['Nate','Rose']],
        'day': [28, 28,28, 18, 18, 18, 20, 20, 3],

        }
df = pd.DataFrame(data)
newdf = df.shift(1).fillna('[]').reset_index().rename(columns={'name':'newcol'})

b=newdf.newcol.tolist()
a=df.name.tolist()


c = [[x for x in ii if  x in jj] for ii, jj in zip(a, b)]
df['name_intersect']=c


Out:
    small group                      all_groups  ... day  name_intersect
0    [a1, a2]  [[a1, a2], [a2, a3], [a3, a4]]  ...  28              []
1    [a2, a3]  [[a1, a2], [a2, a3], [a3, a4]]  ...  28   [Alina, Kate]
2    [a3, a4]  [[a1, a2], [a2, a3], [a3, a4]]  ...  28   [Kate, Diana]
3    [d1, d2]  [[d1, d2], [d2, d3], [d3, d4]]  ...  18              []
4    [d2, d3]  [[d1, d2], [d2, d3], [d3, d4]]  ...  18              []
5    [d3, d4]  [[d1, d2], [d2, d3], [d3, d4]]  ...  18           [Ian]
6    [c1, c2]            [[c1, c2], [c2, c3]]  ...  20         [Petro]
7    [c2, c3]            [[c1, c2], [c2, c3]]  ...  20              []
8    [f1, f2]                      [[f1, f2]]  ...   3              []

If you want to shift the name_intersect column by -1 then

df['name_intersect']=df['name_intersect'].shift(-1).fillna('[]')

Out: 
    

small group                      all_groups                         name  day name_intersect
0    [a1, a2]  [[a1, a2], [a2, a3], [a3, a4]]                [Alina, Kate]   28  [Alina, Kate]
1    [a2, a3]  [[a1, a2], [a2, a3], [a3, a4]]         [Alina, Kate, Diana]   28  [Kate, Diana]
2    [a3, a4]  [[a1, a2], [a2, a3], [a3, a4]]                [Kate, Diana]   28             []
3    [d1, d2]  [[d1, d2], [d2, d3], [d3, d4]]                  [Mike, Bob]   18             []
4    [d2, d3]  [[d1, d2], [d2, d3], [d3, d4]]                  [Ian, Lili]   18          [Ian]
5    [d3, d4]  [[d1, d2], [d2, d3], [d3, d4]]  [George, Cloud, Ian, Petro]   18        [Petro]
6    [c1, c2]            [[c1, c2], [c2, c3]]          [Jone, Petro, Lili]   20             []
7    [c2, c3]            [[c1, c2], [c2, c3]]    [Marinet, Yu, Chloe, Ian]   20             []
8    [f1, f2]                      [[f1, f2]]                 [Nate, Rose]    3             []

Now, delete rows containing empty list in name_intersect and get df_final

df_final=df[df.astype(str)['name_intersect'] != '[]']

out:
         small group                      all_groups                         name  day name_intersect
0    [a1, a2]  [[a1, a2], [a2, a3], [a3, a4]]                [Alina, Kate]   28  [Alina, Kate]
1    [a2, a3]  [[a1, a2], [a2, a3], [a3, a4]]         [Alina, Kate, Diana]   28  [Kate, Diana]
4    [d2, d3]  [[d1, d2], [d2, d3], [d3, d4]]                  [Ian, Lili]   18          [Ian]
5    [d3, d4]  [[d1, d2], [d2, d3], [d3, d4]]  [George, Cloud, Ian, Petro]   18        [Petro]

Entire code:

df = pd.DataFrame(data)
newdf = df.shift(1).fillna('[]').reset_index().rename(columns={'name':'newcol'})

b=newdf.newcol.tolist()
a=df.name.tolist()


c = [[x for x in ii if  x in jj] for ii, jj in zip(a, b)]
df['name_intersect']=c

df['name_intersect']=df['name_intersect'].shift(-1).fillna('[]')

df_final=df[df.astype(str)['name_intersect'] != '[]']
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文