There is one data frame consisting of three columns: group, po, and part
import pandas as pd
df = pd.DataFrame({'group':[1,1,1,1,1,1,2,2,2,2,3,3],
'po':['1a','1b','','','','','2a','2b','2c','','3a',''],
'part':['a','b','c','d','e','f','g','h','i','j','k','l']})
by group column,
Po columns are common parts and I want to display them continuously using a slash(‘/’)
like below dataframe
What should I do?
>Solution :
You can group the po values by group, aggregating them using join (with filter to discard empty values):
df['po'] = df.groupby('group')['po'].transform(lambda g:'/'.join(filter(len, g)))
df
Output:
group po part
0 1 1a/1b a
1 1 1a/1b b
2 1 1a/1b c
3 1 1a/1b d
4 1 1a/1b e
5 1 1a/1b f
6 2 2a/2b/2c g
7 2 2a/2b/2c h
8 2 2a/2b/2c i
9 2 2a/2b/2c j
10 3 3a k
11 3 3a l

