简体   繁体   中英

Get a combination of values in dataframe and implement a function

I want to take the combination of values in a column and apply a function to each combination.What is the easiest way to do this?

Example Data

| name | value |
|------|-------|
| 6A   | 1     |
| 6A   | 1     |
| 6A   | 1     |
| 6B   | 3     |
| 6B   | 3     |
| 6B   | 3     |
| 6C   | 7     |
| 6C   | 5     |
| 6C   | 4     |

The Result I Want
i used sum as a function in the example:

| pair  | result |
|-------|--------|
| 6A_6B | 4      |
| 6A_6B | 4      |
| 6A_6B | 4      |
| 6A_6C | 8      |
| 6A_6C | 6      |
| 6A_6C | 5      |
| 6B_6C | 10     |
| 6B_6C | 8      |
| 6B_6C | 7      |

Note
My function takes "pandas.Series" as parameters.
For example:
x =a series of "6A"
and
y =a series of "6B"

6A_6B = sum(x,y)

I find it more straightforward to reshape the data, then it's a simple addition of all pairwise combinations.

import pandas as pd
from itertools import combinations

u = (df.assign(idx = df.groupby('name').cumcount()+1)
       .pivot(index='idx', columns='name', values='value'))
#name  6A  6B  6C
#idx             
#1      1   3   7
#2      1   3   5
#3      1   3   4

l = []
for items in combinations(u.columns, 2):
    l.append(u.loc[:, items].sum(1).to_frame('result').assign(pair='_'.join(items)))

df = pd.concat(l)

     result   pair
idx               
1         4  6A_6B
2         4  6A_6B
3         4  6A_6B
1         8  6A_6C
2         6  6A_6C
3         5  6A_6C
1        10  6B_6C
2         8  6B_6C
3         7  6B_6C

itertools.combinations

Off the top of my head

from itertools import combinations

g = dict(tuple(df.groupby('name')))

pd.DataFrame([
    (f'{x}_{y}', a + b)
    for x, y in combinations(g, 2)
    for a, b in zip(g[x]['value'], g[y]['value'])
], columns=df.columns)

    name  value
0  6A_6B      4
1  6A_6B      4
2  6A_6B      4
3  6A_6C      8
4  6A_6C      6
5  6A_6C      5
6  6B_6C     10
7  6B_6C      8
8  6B_6C      7

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM