Pandas dataframe manipulating with long string column to 2 columns dynamically

Viewed 88

I have dataframe:

id greetingsname
1 hello+peter|goodbye+john
2 hi+cheff|gutentag+rudolf|goodafternoon+Alex

and i want

id greeting name
1 hello peter
1 goodbye john
2 hi cheff
2 gutentag rudolf
2 goodafternoon Alex

I dont know, how dynamically split column greetingsname, to get what I want, because column greetingsname has different string lengths. But the delimeter distribution remains the same greeting DELIMETER(+) name DELIMETER(|) greeting DELIMETER(+) name

And in this sense it can have different lengths (several names and greetings and in another column a different number of names and greetings)

Thx

2 Answers

You can use explode for this:

df['greetingsname']=df['greetingsname'].apply(lambda x: x.split('|'))

res=df.explode('greetingsname')

print(res)

   id       greetingsname
0   1         hello+peter
0   1        goodbye+john
1   2            hi+cheff
1   2     gutentag+rudolf
1   2  goodafternoon+Alex

res[['greeting', 'name']] = res['greetingsname'].str.split('+', expand=True)

del res['greetingsname']

res.reset_index(drop=True, inplace=True)

print(res)

   id       greeting    name
0   1          hello   peter
1   1        goodbye    john
2   2             hi   cheff
3   2       gutentag  rudolf
4   2  goodafternoon    Alex

We can try extractall

df.set_index('id')['greetingsname']\
  .str.extractall(r'(?P<greeting>[^+|]+)\+(?P<name>[^|]+)').droplevel(1)

         greeting    name
id                       
1           hello   peter
1         goodbye    john
2              hi   cheff
2        gutentag  rudolf
2   goodafternoon    Alex
Related