【问题标题】:Converting a pandas dataframe column values from string to float [duplicate]将熊猫数据框列值从字符串转换为浮点数
【发布时间】:2020-11-17 06:49:56
【问题描述】:

我有一个包含 A、B 和 C 列的数据框,如下所示:

df:

A    B    C

1990 1  1,00,000
2000 2  2,00,000
2001 3  3,00,000

我想将 C 列从对象转换为浮点数。为此,我运行以下命令:

df["C"].astype(float)

它给出了错误:

ValueError: could not convert string to float: '1,00,000'

如何将列转换为浮点数或整数?

【问题讨论】:

  • 只是我或逗号放在了错误的地方..

标签: python pandas dataframe dtype


【解决方案1】:

试试这个:

df["C"].str.replace(",", "").astype(float)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-01
    • 1970-01-01
    • 2019-04-16
    • 2017-09-17
    相关资源
    最近更新 更多