Consider the following data block
df_test = pd.DataFrame( {'a' : [1, 2, 8], 'b' : [np.nan, np.nan, 5], 'c' : [np.nan, np.nan, 4]}) df_test.index = ['one', 'two', 'three']
which gives
abc one 1 NaN NaN two 2 NaN NaN three 8 5 4
I have a row replacement dictionary for columns b and c. For instance:
{ 'one': [3.1, 2.2], 'two' : [8.8, 4.4] }
where 3.1 and 8.8 replace columns b and 2.2 and 4.4 replace column c, so the result
abc one 1 3.1 2.2 two 2 8.8 4.4 three 8 5 4
I know how to make these changes to the for loop:
index_list = ['one', 'two'] value_list_b = [3.1, 8.8] value_list_c = [2.2, 4.4] for i in range(len(index_list)): df_test.ix[df_test.index == index_list[i], 'b'] = value_list_b[i] df_test.ix[df_test.index == index_list[i], 'c'] = value_list_c[i]
but I'm sure there is a more convenient and faster way to use the dictionary!
I think this can be done using the DataFrame.replace method, but I could not figure it out.
Thanks for the help,
CD