大熊猫填写N.A.特定栏目

时间:2016-08-29 11:28:52

标签: python pandas fill na imputation

如果在另一列中满足条件,我想在特定列中填写N.A.值,以仅用插入/替换值替换此单个N.A.值类别。

E.g。我想表演:if column1 = 'value1' AND column2 = N.A fillna_in_column2 with value 'replacementvalue'

我如何在熊猫中实现这一目标?

尝试通过dataframe[dataframe['firstColumn'] == 'value1'].fillna({'column2':'replacementValue'}执行此操作不起作用,因为修改了整个记录的长度。到目前为止,我无法进行适当的修改工作。

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:1)

你可以试试这个:

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

df['column2'][cond1 & cond2] = replacement_value