ScientiaEtVeritas
ScientiaEtVeritas

Reputation: 5278

Replace Some Columns of DataFrame with Another (Based on Column Names)

I have a DataFrame df1:

| A | B | C | D |
-----------------
| 0 | 1 | 3 | 4 |
| 2 | 1 | 8 | 4 |
| 0 | 2 | 3 | 1 |

and a DataFrame df2:

| A | D |
---------
| 2 | 2 |
| 3 | 2 |
| 1 | 9 |

I want to replace column A and D of df1 with the equivalent columns of df2.

Surely I could do something like

df1['A'] = df2['A']
df1['D'] = df2['D']

But I need a solution for doing this automatically since I have thousands of columns.

Upvotes: 3

Views: 1081

Answers (4)

piRSquared
piRSquared

Reputation: 294218

The way to do this is with pd.DataFrame.update
Update will modify a dataframe in place with information in another dataframe.

df1.update(df2)

The advantage of this is that your dtypes in df1 are preserved.

df1

   A  B  C  D
0  2  1  3  2
1  3  1  8  2
2  1  2  3  9

Another way to have done this with out updating in place would be to have used pd.DataFrame.assign and dictionary unpacking on pd.DataFrame.iteritems. However, this would include new additional columns if they existed in df2.

df1.assign(**dict(df2.iteritems()))

   A  B  C  D
0  2  1  3  2
1  3  1  8  2
2  1  2  3  9

Upvotes: 5

Stael
Stael

Reputation: 2689

for col in df1.columns:
    if col in df2.columns.tolist():
        df1[col] = df2[col]

Upvotes: 0

akuiper
akuiper

Reputation: 214927

You can use combine_first:

df2.combine_first(df1)

#   A   B   C   D
#0  2   1.0 3.0 2
#1  3   1.0 8.0 2
#2  1   2.0 3.0 9

Upvotes: 6

acushner
acushner

Reputation: 9946

a simple for loop should suffice:

for c in df2.columns:
    df1[c] = df2[c]

Upvotes: 1

Related Questions