【问题标题】:I am trying to replace data within a specific set of rows in a specific column with NaN using Pandas我正在尝试使用 Pandas 用 NaN 替换特定列中特定行集中的数据
【发布时间】:2021-12-29 06:16:24
【问题描述】:

我有一个数据集,url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'

我正在尝试用“NaN”替换第 10 到 17 行。

这是我的代码:

iris['petal_length'] = iris['petal_length'].between(10,17).replace('NaN')
iris.iloc[10:18]

【问题讨论】:

  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。

标签: python pandas dataframe


【解决方案1】:

我们可以使用 loc

import numpy as np
df.loc[10:18,'petal length (cm)'] = np.nan
print(df.loc[10:18])

输出:

sepal length (cm)  sepal width (cm)  petal length (cm)  petal width (cm)
5.4               3.7                NaN               0.2
4.8               3.4                NaN               0.2
4.8               3.0                NaN               0.1
4.3               3.0                NaN               0.1
5.8               4.0                NaN               0.2
5.7               4.4                NaN               0.4
5.4               3.9                NaN               0.4
5.1               3.5                NaN               0.3
5.7               3.8                NaN               0.3

【讨论】:

  • 谢谢阿布舍克!非常感激。我不得不更改一些东西(参考数据帧),但整体语法很有效。
猜你喜欢
  • 2018-12-22
  • 2020-01-16
  • 1970-01-01
  • 2020-02-04
  • 1970-01-01
  • 2021-10-04
  • 1970-01-01
  • 2021-06-09
  • 2018-12-20
相关资源
最近更新 更多