目前的熊猫版: 0.22
我有一个SparseDataFrame.
A = pd.SparseDataFrame(
[['a',0,0,'b'],
[0,0,0,'c'],
[0,0,0,0],
[0,0,0,'a']])
Run Code Online (Sandbox Code Playgroud)
A
0 1 2 3
0 a 0 0 b
1 0 0 0 c
2 0 0 0 0
3 0 0 0 a
Run Code Online (Sandbox Code Playgroud)
现在,填充值是0.但是,我想将fill_values更改为np.nan.我的第一直觉是打电话replace:
A.replace(0, np.nan)
Run Code Online (Sandbox Code Playgroud)
但这给了
TypeError: cannot convert int to an sparseblock
Run Code Online (Sandbox Code Playgroud)
这并没有真正帮助我理解我做错了什么.
我知道我能做到
A.to_dense().replace(0, np.nan).to_sparse()
Run Code Online (Sandbox Code Playgroud)
但有更好的方法吗?或者我对稀疏数据帧的基本理解存在缺陷?