【问题标题】:How to transform an entire colum with strings in numbers? [duplicate]如何用数字字符串转换整个列? [复制]
【发布时间】:2020-06-27 08:23:53
【问题描述】:

怎么了?

在 excel 中,我可以使用 number_value 公式将一整列日期或字符串转换为数字。我可以在 Python 中使用我的 dataFrames 上的 pandas 做这样的事情吗?

Tks

【问题讨论】:

  • 是的,请参阅 pd.to_numericdf[col].astype(int) or (float)

标签: pandas


【解决方案1】:

下面的代码应该将字符串值转换为数值。只有在将日期从字符串转换为数字时才需要第一行。

df['col'] = pd.to_datetime(df['col'])
df['col'] = pd.to_numeric(df['col'])

【讨论】:

    猜你喜欢
    • 2011-03-25
    • 2020-10-24
    • 2012-04-30
    • 2019-10-16
    • 2021-06-11
    • 2019-03-01
    • 1970-01-01
    • 2014-02-22
    相关资源
    最近更新 更多