The data frame consists of id and title as shown below.
I want to pair title items that belong to the same id.
Initial Data Frame
id title
1 'A'
1 'B'
1 'C'
2 'D'
2 'E'
3 'F'
3 'G'
3 'H
Result Data Frame
id title_1 title_2
1 'A' 'B'
1 'B' 'C'
1 'C' 'A'
2 'D' 'E'
3 'G' 'H'
I can't say I used Panda's, but take a look at it all the way.
>>> import pandas as pd
>>> from itertools import combinations
>>> df = pd.DataFrame({ "id":[1,1,1,2,2,3,3,3], "title":list("ABCDEFGH") })
>>> df
id title
0 1 A
1 1 B
2 1 C
3 2 D
4 2 E
5 3 F
6 3 G
7 3 H
>>> df[df["id"] == 1]["title"]
0 A
1 B
2 C
Name: title, dtype: object
>>> list(combinations(df[df["id"]==1]["title"], 2))
[('A', 'B'), ('A', 'C'), ('B', 'C')]
>>> for _id in df["id"].unique():
print(_id)
1
2
3
>>> for _id in df["id"].unique():
for pair in combinations(df[df["id"]==_id]["title"], 2):
print(_id, pair)
1 ('A', 'B')
1 ('A', 'C')
1 ('B', 'C')
2 ('D', 'E')
3 ('F', 'G')
3 ('F', 'H')
3 ('G', 'H')
>>> data = []
>>> for _id in df["id"].unique():
for pair in combinations(df[df["id"]==_id]["title"], 2):
data.append({ "id":_id, "title_1":pair[0], "title_2":pair[1] })
>>> df2 = pd.DataFrame(data)
>>> df2
id title_1 title_2
0 1 A B
1 1 A C
2 1 B C
3 2 D E
4 3 F G
5 3 F H
6 3 G H
© 2024 OneMinuteCode. All rights reserved.