为了账号安全,请及时绑定邮箱和手机立即绑定

如果列包含 Pandas 中的字符,请更改列类型

如果列包含 Pandas 中的字符,请更改列类型

波斯汪 2021-09-14 20:36:18
例如,我在某些列(非空对象)中有字符“%”的值 col1     col2  col3 '4.24%' '5.22%'  8但我想要 4.24 和 5.22 作为浮点数。I have tried with:for el in df.columns:    if df[el].str.contains('%').any():        df[el] = df[el].str.strip("%").astype(float) 并表示 : AttributeError: Can only use .str accessor with string values, which use np.object_ dtype in pandas如果我使用:if df['col1'].str.contains('%').any():            df['col1'] = df['col1'].str.strip("%").astype(float)然后工作正常。但是通过所有列的迭代不起作用。
查看完整描述

2 回答

  • 2 回答
  • 0 关注
  • 219 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信