How to replace part of the data-frame with another data-frame

Viewed 535

i have two data frames and i like to filter the data and replace list of columns from df1 with the same columns in df2

i want to filter this df by df1.loc[df1["name"]=="A"]

first_data={"col1":[2,3,4,5,7],
"col2":[4,2,4,6,4],
"col3":[7,6,9,11,2],
"col4":[14,11,22,8,5],
"name":["A","A","V","A","B"],
"n_roll":[8,2,1,3,9]}
df1=pd.DataFrame.from_dict(first_data)

and put the columns ["col1","col2","n_roll"] when name="A" on the same places in df2 (on the same indexs)

sec_df={"col1":[55,0,57,1,3],
"col2":[55,0,4,4,53],
"col3":[55,33,9,0,2],
"col4":[55,0,22,4,5],
"name":["A","A","V","A","B"],
"n_roll":[8,2,1,3,9]}
df2=pd.DataFrame.from_dict(sec_df)

if i put that list of cols=[col1,col2,col3,col4]

so i like to get this

data={"col1":[55,0,4,1,7],
"col2":[55,0,4,4,4],
"col3":[55,33,9,0,2],
"col4":[55,0,22,4,5],
"name":["A","A","V","A","B"],
"n_roll":[8,2,1,3,9]}
df=pd.DataFrame.from_dict(data)
df
2 Answers
  1. You can achieve this with a double combine_first
  2. Combine a filtered version of df1 with df2
  3. However, the columns that were excluded in the filtered version of df1 were left behind and you have NaN values. But, that is okay -- Just do another combine_first on df2 to get those values!

(df1.loc[df1['name'] != 'A', ["col1","col2","n_roll"]]
 .combine_first(df2)
 .combine_first(df2))
Out[1]: 
   col1  col2  col3  col4  n_roll name
0  55.0  55.0  55.0  55.0     8.0    A
1   0.0   0.0  33.0   0.0     2.0    A
2   4.0   4.0   9.0  22.0     1.0    V
3   1.0   4.0   0.0   4.0     3.0    A
4   7.0   4.0   2.0   5.0     9.0    B

Use one line to achieve this man!

df1=df1[df1.name!='A'].append(df2[df2.name=='A'].rename(columns={'hight':'n_roll'})).sort_index()



   col1  col2  col3  col4 name  n_roll
0    55    55    55    55    A       8
1     0     0    33     0    A       2
2     4     4     9    22    V       1
3     1     4     0     4    A       3
4     7     4     2     5    B       9

How it works

d=df1[df1.name!='A']#selects df1 where name is not A

df2[df2.name=='A']#selects df where name is A

e=df2[df2.name=='A'].rename(columns={'hight':'n_roll'})#renames column height to allow appending

d.append(e)# combines the dataframes

d.append(e).sort_index()#sorts the index
Related