if few columns are the same and then append the row value together in pandas
Question:
The following are the details
Here is the data frame
Name| Filename| delimetier| good delimeter| bad delimeter
A 123 48 a A
A 123 48 A
B 123 48 b C
C 123 49 c B
A 123 48 d D
A 123 48 c E
B 123 48 d F
What I want is
Name| Filename| delimetier| good delimeter| bad delimeter
A 123 48 a, c, d A, D, E
B 123 48 b, d C, F
C 123 49 c B
Even there have null value and duplicates, ignore them. And I have tried use groupby() to solve it, but failed.
Answers:
You can use a groupby.apply
to achieve this result.
Using this data:
>>> df
Name Filename delimeter good delimeter
0 A 123 48 a
1 B 123 48 b
2 C 123 49 c
3 A 123 48 d
4 A 123 48 c
5 B 123 48 d
Solution
out = (
df.groupby(['Name', 'Filename', 'delimeter'], as_index=False)
['good delimeter'].apply(', '.join)
)
print(out)
Name Filename delimeter good delimeter
0 A 123 48 a, d, c
1 B 123 48 b, d
2 C 123 49 c
The following are the details
Here is the data frame
Name| Filename| delimetier| good delimeter| bad delimeter
A 123 48 a A
A 123 48 A
B 123 48 b C
C 123 49 c B
A 123 48 d D
A 123 48 c E
B 123 48 d F
What I want is
Name| Filename| delimetier| good delimeter| bad delimeter
A 123 48 a, c, d A, D, E
B 123 48 b, d C, F
C 123 49 c B
Even there have null value and duplicates, ignore them. And I have tried use groupby() to solve it, but failed.
You can use a groupby.apply
to achieve this result.
Using this data:
>>> df
Name Filename delimeter good delimeter
0 A 123 48 a
1 B 123 48 b
2 C 123 49 c
3 A 123 48 d
4 A 123 48 c
5 B 123 48 d
Solution
out = (
df.groupby(['Name', 'Filename', 'delimeter'], as_index=False)
['good delimeter'].apply(', '.join)
)
print(out)
Name Filename delimeter good delimeter
0 A 123 48 a, d, c
1 B 123 48 b, d
2 C 123 49 c