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.

Asked By: huo shankou

||

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
Answered By: Cameron Riddell
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.