python - 加入带有附加符号的系列的最佳方法是什么,而忽略了 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
我应该怎么办?
解决方案
只需更换垫片:
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 锉
推荐阅读
- javascript - 获取 HTML 的值
- javascript - javascript/HTML:以对象作为 HTML 字符串文字中的参数绑定事件处理函数
- apache-kafka - flink 检查点和 kafka 生产者完全一次
- stripe-payments - 印度管理员账户时条纹支付不完整
- c# - 添加迁移时无法加载文件或程序集网络标准
- c++ - 如何描述继承的类?
- java - javafx.fxml.FXMLLoader$ValueElement processValue 警告:使用版本 8.0.171 的 JavaFX 运行时使用版本 11.0.1 的 JavaFX API 加载 FXML 文档
- r - 如何对具有相同名称 r studio 的所有值求和?
- javascript - 如何使用正则表达式进行多次替换,其中要替换的术语存储在变量中?
- chat - 在 AIML 中与不同用户进行多次对话