Reputation: 139
Let´s say I have a DataFrame df_a and another one df_b.
I want to replace lines 42 to 51 of df_a with the corresponding rows of df_b (same number of rows, but more columns as df_a).
My current code is
df_a.loc[45:52,:] = df_b.loc[45:52,"Column A":"Column D"]
where "Column A":"Column D" matches the total number of columns of df_a.
I´m getting an unintented result where all selected terms in df_a get replaced by nan instead of df_b values. Does anyone know an easy way to do it?
Upvotes: 1
Views: 151
Reputation: 470
df_1 = pd.DataFrame({"a" : (1,2,3,4), "b" : (5,4,6,7)})
df_2 = pd.DataFrame({"a" : (1,2,3,5), "b" : (2,3,6,3), "c" : (10,11,12,13)})
print(df_1)
a b
0 1 5
1 2 4
2 3 6
3 4 7
print(df_2)
a b c
0 1 2 10
1 2 3 11
2 3 6 12
3 5 3 13
df_1.iloc[2,] = df_2.iloc[3,1:3].values
a b
0 1 5
1 2 4
2 3 13
3 4 7
In this example I replaced the values of the 3rd row of df_1 with the last 2 values of the 4th row of df_2
Upvotes: 1