Skip to content Skip to sidebar Skip to footer

Python Pandas How To Combine The Pandas With The Same Column Value

convert this frame: 1, 2 ---- a, g a, a a, j d, b c, e into: 1, 2 ---- a, g,a,j d, b c, e what can I do, can I use groupby? what other methods?

Solution 1:

You can use groupby with apply function join:

df.columns = list('AB')
print (df)
   A  B
0  a  g
1  a  a
2  a  j
3  d  b
4  c  e

df = df.groupby('A')['B'].apply(','.join).reset_index()
print (df)
   A      B
0  a  g,a,j
1  c      e
2  d      b

Solution 2:

pivot_table

df.pivot_table('B', 'A', aggfunc=','.join)

A
a    g,a,j
c        e
d        b
Name: B, dtype: object

Solution 3:

There might be better ways but one of the ways can be like this:

import pandas as pd
import re

# df = pd.read_clipboard()
df2 = df.copy()
df2.columns=['col1','col2']
df2=df2.groupby('col1',as_index=False).sum()
df2.col2 = df2.col2.apply(lambda x: re.sub(r'(.)',r'\1,', x) if len(x)>1 else x)
df2

Output:

enter image description here


Solution 4:

If you want to keep g,a,j as a python list then you can use apply to a function that returns either a list or a scalar depending on the number of elements:

df = pd.DataFrame({'A':list('aaadc'), 'B':list('gajbe')})

df = df.groupby('A')['B'].apply(lambda s: list(s) if len(s)>1 else s.iloc[0]).reset_index()
print (df)

Outputs:

   A          B
0  a  [g, a, j]
1  c          e
2  d          b

Post a Comment for "Python Pandas How To Combine The Pandas With The Same Column Value"