我有以下由整数值组成的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
答案 0 :(得分:7)
如果设置了df.loc[1,'a'] = np.nan
print (df)
a b
1 NaN 1
2 1.0 1
3 1.0 1
,函数DataFrame.at
似乎不能将整数转换为浮点数。
为我工作DataFrame.loc
:
loc
@Peter Leimbigler的解释:
首先需要进行类型转换的原因是因为 nan 是 float 类型,并且是 int 数据类型不支持 nan 或任何其他缺失值。为了使数字列包含 nan ,它的类型必须为 float 。
@pir解释:
pandas.DataFrame.at 已针对特定单元格访问进行了优化。因此,它不能更改列的dtype。但是,
var a=[ {name: "abc", address: "xxx"} , {name: "def", address: "yyy"} , {name: "xyz", address: "xyz"} , {name: "abc", address1: "123"} , {name: "def", address1: "456"} ] var result = Object.values(a.reduce( (o,d) => ( o[d.name] = { ...d, ...(o[d.name] && o[d.name]) } , o ) , {}) ) console.log(result)
可以。
答案 1 :(得分:5)
添加java.lang.NegativeArraySizeException
astype