Use of loc to update a dataframe python pandas

While not being the most beautiful, the way I would achieve your goal (without explicitly iterating over the rows) is:

df.ix[df['month'] == 'Feb', 'a'] = df[df['month'] == 'Feb']['b'] + df[df['month'] == 'Feb']['c']  

Note: ix has been deprecated since Pandas v0.20.0 in favour of iloc / loc.


As you could see from the warning you should use loc[row_index, col_index]. When you subsetting your data you get index values. You just need to pass for row_index and then with comma col_name:

df.loc[df['month'] == 'Feb', 'A'] = df.loc[df['month'] == 'Feb', 'B'] + df.loc[df['month'] == 'Feb', 'C'] + df.loc[df['month'] == 'Feb', 'D']