首页 > 解决方案 > 加入带有附加符号的系列的最佳方法是什么,而忽略了 none 和 nan 值?

问题描述

我有一个这样的系列

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

这是我的代码

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']

x2 的结果是

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

我想要的结果是

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

我应该怎么办?

标签: pythonpandasstring

解决方案


只需更换垫片:

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

输出:

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

推荐阅读