Sorry If my English is bad(while trying to explain). I have two different data frames. Say my df1 is,
a b d 0 1 2 5 1 2 3 9 2 3 4 1 3 1 2 5 4 2 3 9 5 3 4 1 6 1 2 5 7 2 3 9 8 3 4 1 Second df is,
xyz 0 23 1 24 2 35 6 17 7 23 8 34 Now, I only want to replace the values in my df1(column 'a') based on the index of df2.suppose index 0 of df2 has 23 then df1 index 0 has to be changed to 23. I have tried it but I am getting an error. How do I replace the values. My code is below:-
for i in df1.index: for j indf2.index: if i == j: df1.iloc['a'][i] = df2.loc['xyz'][j] print(df1) getting a error like
KeyError: 'xyz' During handling of the above exception, another exception occurred: How would I resolve this, Where am I going wrong? Output will like this:
a b d 0 23 2 5 1 24 3 9 2 35 4 1 3 1 2 5 4 2 3 9 5 3 4 1 6 17 2 5 7 23 3 9 8 34 4 1