gpt4 book ai didi

python - 在 Pandas 的整数数据框中使用 DataFrame.at 分配 nan 值时出现 ValueError

转载 作者:太空宇宙 更新时间:2023-11-04 06:51:40 25 4
gpt4 key购买 nike

我有以下由整数值组成的 DataFrame:

df = pd.DataFrame(data=1, columns=['a','b'], index=[1,2,3])

a b
1 1 1
2 1 1
3 1 1

我想在单个单元格上设置缺失值,当我尝试时:

df.at[1,'a'] = np.nan

然后我得到这个异常:

Traceback (most recent call last):
File "<input>", line 1, in <module>
File "pandas/core/indexing.py", line 2159, in __setitem__
self.obj._set_value(*key, takeable=self._takeable)
File "pandas/core/frame.py", line 2582, in _set_value
engine.set_value(series._values, index, value)
File "pandas/_libs/index.pyx", line 124, in pandas._libs.index.IndexEngine.set_value
File "pandas/_libs/index.pyx", line 133, in pandas._libs.index.IndexEngine.set_value
File "pandas/_libs/index.pyx", line 570, in pandas._libs.index.convert_scalar
ValueError: Cannot assign nan to integer series

最佳答案

好像函数DataFrame.at如果设置 NaN,则无法将整数转换为 float 。

为我工作DataFrame.loc :

df.loc[1,'a'] = np.nan
print (df)
a b
1 NaN 1
2 1.0 1
3 1.0 1

@Peter Leimbigler 解释:

The reason why any type-casting is needed in the first place is because nan is of type float, and the int data type has no support for nan or any other missing value. In order for a numeric column to contain nan, it must be of type float.

@pir 解释:

pandas.DataFrame.at is optimized for specific cell access. Therefore it cannot change the dtype of the column. However, loc can.

关于python - 在 Pandas 的整数数据框中使用 DataFrame.at 分配 nan 值时出现 ValueError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53121403/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com