pandas 填写 N.A。对于特定列

pandas fill N.A. for specific column

我要填写N.A。如果在另一列中满足条件,则特定列中的值将仅替换 N.A 中的单个 class。具有估算值/替换值的值。

例如我要表演:if column1 = 'value1' AND column2 = N.A fillna_in_column2 with value 'replacementvalue'

如何在 pandas 中实现此目的?

尝试通过 dataframe[dataframe['firstColumn'] == 'value1'].fillna({'column2':'replacementValue'} 执行此操作无效,因为修改了总记录的长度。到目前为止,我无法进行就地修改。

你可以试试这个:

cond1 = df['column1'] == value1
cond2 = np.isnan(df['column2'])

df['column2'][cond1 & cond2] = replacement_value
df.loc[(df['col1']==val1) & (df['col2'].isnull()), 'col2'] = replacement_value