python - 清理两列,然后合并为一个值
问题描述
我正在尝试将两列合并为一个,但首先我必须清理它们。我有的:
Index | Col1 | Col2
1 | 1 in | 1.0
2 | 2 inches | 2.001
3 | NA | NA
我想要的是:
Index | Merged
1 | 1
2 | 2
3 |
对于 Col2,我必须掩盖 NA 值以四舍五入。
na_mask = df['Col2'].notnull()
df.loc[na_mask, 'Col2'] = power.loc[na_mask, 'Col2'].astype(float).round(0).astype(int)
对于 Col1,我已经能够删除 col 中的字符,但是当我尝试将其转换为 Int 时出现错误
df['Col1']=df['Col2'].apply(lambda measure: re.sub(r'[a-z]|[A-z]|\"','',str(measure))).astype(int)
最后我加入了这两列,但如果有更简单的方法,请告诉我:
df[['Col1','Col2']].apply(lambda x: ",".join(set([str(i) for i in x if pd.notnull(i)])), axis=1)
解决方案
推荐阅读
- sql - 字段名称似乎不会延续到交叉表查询中
- azure - PowerShell Azure Functions 多租户查询
- javascript - HTML/JavaScript 倒数计时器
- reactjs - 当鼠标悬停时,navdropdown 在 React 中出现错误
- android - Glide 加载错误的图像尺寸
- python - Sympy nsolve 用于联立方程化学平衡
- swift - 使用烟雾粒子模板的雾效果
- svm - SVM 中核函数的参数是什么?
- mysql - 如何在 JFXTable 视图中添加按钮编辑和删除以及如何将行编辑到 SQL
- python - 正则表达式结合 2 个不同行的匹配信息