【问题标题】:How to remove spesific things from data in Python如何从 Python 中的数据中删除特定的东西
【发布时间】:2021-10-06 14:01:00
【问题描述】:

我有这样的数据:

draft_round
    0   1st round
    1   3rd round
    2   1st round
    3   16th round
    4   2nd round
    ... ...
    4680    1st round
    4681    NaN
    4682    2nd round
    4683    2nd round
    4684    1947 BAA Draf

如您所见,每一行数据都有复杂的数据,是单词和数字的组合。对我来说重要的是得到这些行中的数字。例如,我想在名为“第 1 轮”的数据行中获取数字“1”,在“第 16 轮”数据行中获取“16”。换句话说,我希望产量如下:

      draft_round
        0   1
        1   3
        2   1
        3   16
        4   2
        ... ...
        4680    1
        4681    NaN
        4682    2
        4683    20
        4684    1947 BAA Draf

我希望我能够解释我的问题,提前谢谢。

【问题讨论】:

  • 你也可以使用re库中的sub

标签: python python-3.x pandas dataframe data-science


【解决方案1】:

你可以试试.str.replace:

df["draft_round"] = df["draft_round"].str.replace(
    r"(\d+).*round", r"\1", regex=True
)
print(df)

打印:

        draft_round
0                 1
1                 3
2                 1
3                16
4                 2
4680              1
4681            NaN
4682              2
4683              2
4684  1947 BAA Draf

【讨论】:

  • 非常感谢先生,感谢您的帮助。我刚刚从您那里学到了一些新东西。
【解决方案2】:

尝试str.split

df['draft_round'] = df['draft_round'].str.split(pat='[a-z]', expand=True)[0]

【讨论】:

  • 这非常有用,先生,谢谢。但是像“1968 NBA D”这样的一些数据并没有被删除。您对此有所了解吗?
  • 数据看起来与“draft_round”无关。您可以简单地删除该行数据。
猜你喜欢
  • 2015-01-14
  • 1970-01-01
  • 2014-12-15
  • 1970-01-01
  • 2018-07-18
  • 2020-09-13
  • 1970-01-01
  • 1970-01-01
  • 2022-12-19
相关资源
最近更新 更多