Ali Khatami
Ali Khatami

Reputation: 379

How to transform column values of a dataframe to another dataframe with different indexes?

I have two dataframes with equal number of rows.

df_a:

index   Id   X   Y
1       10   x1  - 
2       20   x2  -

-----------

df_b:

index   PostId   Text   
3       10       abcd
4       20       efg

Now how can I transform values of df_b['Text'] to df_a['Y']. resulting this:

df_a:

index   Id   X   Y
1       10   x1  abcd
2       20   x2  efg

Note that indexes of mentioned dataframes are not the same.

Upvotes: 0

Views: 42

Answers (1)

jezrael
jezrael

Reputation: 863116

Because of the same number of rows, you can assign numpy array:

df_a['Y'] = df_b['Text'].to_numpy()

Older pandas versions:

df_a['Y'] = df_b['Text'].values

If want to map or merge be free to use some of solution from this answer.

Upvotes: 1

Related Questions