0

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 

1 Answer 1

2

Does this help Rapooram?

df = pd.DataFrame({'a' : [1,2,3,1,2,3,1,2,3], 'b' : [2,3,4,2,3,4,2,3,4], 'd' : [5,9,1,5,9,1,5,9,1]}) df2 = pd.DataFrame({'index' : [0,1,2,6,7,8] , 'xyz' : [23,24,35,17,23,43]}) df2.set_index('index',inplace=True) df.loc[df2.index,['a']] = df2.xyz 
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, it did. Thanks :) will vote once I get it

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.