【问题标题】:How to convert all int convertible data to int in pandas?如何将所有 int 可转换数据转换为 pandas 中的 int?
【发布时间】:2017-06-22 03:18:16
【问题描述】:

我有 pandas DF,它有几个 int type 数据作为字符串和浮点数。 我想将所有可以转换为int的数据转换为int,否则保持原样。

# something like
df = df.applymap(lambda x: int(x) if type(int(x)) is int else x)

# this code is wrong and is something like x/y problem. But, is there a easy way workout this problem.

谢谢,

【问题讨论】:

  • 你不能在 pandas 列中使用异构类型,除非你使用 object dtype。这不是一件好事。

标签: python pandas dataframe lambda int


【解决方案1】:

你可以使用 isdigit()

df.applymap(lambda x: 'int' if str(x).isdigit() else x)

【讨论】:

  • 有问题的:'-10''0x1234''1e10'
猜你喜欢
  • 1970-01-01
  • 2014-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-25
  • 2019-06-18
  • 2021-11-02
相关资源
最近更新 更多