[英]Replace null values in pandas data frame column with 2D np.zeros() array
Assume the following data frame:假设以下数据框:
import pandas as pd
import numpy as np
vals = [1, 2, 3, 4, 5]
df = pd.DataFrame({'val': vals})
df['val'][[0, 3]] = np.nan
Gives:给出:
val
0 NaN
1 2.0
2 3.0
3 NaN
4 5.0
I need to be able to replace NaN
values in the val
column with a 2D numpy array of zeros.我需要能够用 2D numpy 零数组替换
val
列中的NaN
值。 When I do the following:当我执行以下操作时:
z = np.zeros((10, 10))
df['val'][df['val'].isnull()] = z
The arrays are converted to scalars of value 0.0: arrays 被转换为值为 0.0 的标量:
val
0 0.0
1 2.0
2 3.0
3 0.0
4 5.0
I really need the array to be maintained (in this case, each NaN
value - rows 0 and 3 from the original data frame - should be replaced with a 10x10 array of zeros).我真的需要维护数组(在这种情况下,每个
NaN
值 - 来自原始数据帧的第 0 行和第 3 行 - 应该替换为 10x10 的零数组)。 I've tried converting to object
type first我尝试先转换为
object
类型
df = df.astype(object)
df['val'][df['val'].isnull()] = z
With no success.没有成功。 Whhyyyyy
为什么
It is cause by the object data type we have a way with fillna
这是由 object 数据类型引起的,我们有办法使用
fillna
df.val.fillna(dict(zip(df.index[df['val'].isnull()],[z]*df['val'].isnull().sum())),inplace=True)
df
val
0 [[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,...
1 2.0
2 3.0
3 [[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,...
4 5.0
You were really close.你真的很亲近。 Change the dataframe's dtype to
object
and change = z
to = [z]
:将数据帧的 dtype 更改为
object
并将= z
更改为= [z]
:
df = df.astype(object)
df.loc[df['val'].isna(), 'val'] = [z]
Output: Output:
>>> df
val
0 [[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,...
1 2.0
2 3.0
3 [[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,...
4 5.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.