Issue
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
Solution
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
Answered By - IoaTzimas
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.