I’m trying to create duplicate rows during a dataframe on conditions.
For example, I have this Dataframe.
JavaScript
x
12
12
1
students = [
2
("a", "Ursula"),
3
("b", "Hayfa, Martin"),
4
("c", "Kato"),
5
("d", "Tanek, Ava, Pyto"),
6
("e", "Aiko"),
7
("f", "Hunter"),
8
("g", "Josiah, Derek, Uma, Nell"),
9
]
10
df = pd.DataFrame(students, columns=["team", "student"])
11
print(df)
12
JavaScript
1
9
1
team student
2
a Ursula
3
b Hayfa, Martin
4
c Kato
5
d Tanek, Ava, Pyto
6
e Aiko
7
f Hunter
8
g Josiah, Derek, Uma, Nell
9
And I would like to get the following output:
JavaScript
1
15
15
1
team student name remark
2
a Ursula Ursula
3
b Hayfa, Martin Hayfa with Martin
4
b Hayfa, Martin Martin with Hayfa
5
c Kato Kato
6
d Tanek, Ava, Pyto Tanek with Ava, Pyto
7
d Tanek, Ava, Pyto Ava with Tanek, Pyto
8
d Tanek, Ava, Pyto Pyto with Tanek, Ava
9
e Aiko Aiko
10
f Hunter Hunter
11
g Josiah, Derek, Uma, Nell Josiah with Derek, Uma, Nell
12
g Josiah, Derek, Uma, Nell Derek with Josiah, Uma, Nell
13
g Josiah, Derek, Uma, Nell Uma with Josiah, Derek, Nell
14
g Josiah, Derek, Uma, Nell Nell with Josiah, Derek, Uma
15
Advertisement
Answer
For pandas 0.25+ is possible use DataFrame.explode
with splitted values by Series.str.split
and for remark
column list comprehension with filtering:
JavaScript
1
9
1
students = df["student"].str.split(", ")
2
df = df.assign(name=students, remark=students).explode("name").reset_index(drop=True)
3
df["remark"] = [
4
"with " + ", ".join(x for x in r if x != n) if len(r) > 1 else ""
5
for n, r in zip(df["name"], df["remark"])
6
]
7
8
print (df)
9
And we get the following result:
JavaScript
1
15
15
1
team student name remark
2
0 a Ursula Ursula
3
1 b Hayfa, Martin Hayfa with Martin
4
2 b Hayfa, Martin Martin with Hayfa
5
3 c Kato Kato
6
4 d Tanek, Ava, Pyto Tanek with Ava, Pyto
7
5 d Tanek, Ava, Pyto Ava with Tanek, Pyto
8
6 d Tanek, Ava, Pyto Pyto with Tanek, Ava
9
7 e Aiko Aiko
10
8 f Hunter Hunter
11
9 g Josiah, Derek, Uma, Nell Josiah with Derek, Uma, Nell
12
10 g Josiah, Derek, Uma, Nell Derek with Josiah, Uma, Nell
13
11 g Josiah, Derek, Uma, Nell Uma with Josiah, Derek, Nell
14
12 g Josiah, Derek, Uma, Nell Nell with Josiah, Derek, Uma
15