Home > Net >  what is the best way to join series with additional symbol and left out none and nan value? [duplica
what is the best way to join series with additional symbol and left out none and nan value? [duplica

Time:09-16

I got a series like this

0             stand and on the top of the m
1                        be aware of the p
2                           in the night o
3                                       tt
4

Here is my code

x1=x.str.split(pat='/').str[0].copy()
x2=x1.str.split(expand=True).copy()
x2['combined']=x2[x2.columns].apply(lambda row: ' '.join(row.values.astype(str)), axis=1)
x2['combined']

the result of x2 is

0             stand and on the top of the m
1             be aware of the p None None None
2             in the night o None None None None
3             tt None None None None None None None
4             Nan Nan Nan Nan Nan Nan Nan Nan

The outcome I want is

0             stand and on the top of the m
1             be aware of the p
2             in the night o
3             tt
4                  

what should I do?

CodePudding user response:

Just replace the spacer:

x.str.replace('\s ', ' ', regex=True)

output:

0    stand and on the top of the m
1                be aware of the p
2                   in the night o
3                               tt
4

CodePudding user response:

Use:

x['combined']=x.str.split(pat='/').str[0].str.split().str.join(' ')
  • Related