0

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.

ansev
  • 30,322
  • 5
  • 17
  • 31
Ali Khatami
  • 369
  • 2
  • 16

1 Answers1

1

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.

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • Thank you for always sharing knowledge :) – Celius Stingher Feb 06 '20 at 11:26
  • this is trivial – ansev Feb 06 '20 at 11:27
  • @jezrael there is a warning happening: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead – Ali Khatami Feb 06 '20 at 11:28
  • @AliKhatami - How is created `df_a` and `df_b` ? – jezrael Feb 06 '20 at 11:29
  • @AliKhatami - Maybe is necessary `copy` like explained [here](https://stackoverflow.com/questions/48113905/slice-a-pandas-dataframe-based-on-the-results-of-a-function-on-a-column/48113923#48113923) or [here](https://stackoverflow.com/questions/20625582/how-to-deal-with-settingwithcopywarning-in-pandas) – jezrael Feb 06 '20 at 11:31
  • https://stackoverflow.com/questions/45747589/copying-a-column-from-one-dataframe-to-another-gives-nan-values – ansev Feb 06 '20 at 11:32
  • @AliKhatami - Can you edit question with your full code? – jezrael Feb 06 '20 at 11:33
  • @ansev - thank you, I cannot close, because reopend – jezrael Feb 06 '20 at 11:34
  • @ansev - because I wait for [this](https://stackoverflow.com/questions/60093789/how-to-transform-column-values-of-a-dataframe-to-another-dataframe-with-differen/60093837?noredirect=1#comment106283358_60093837) :( – jezrael Feb 06 '20 at 11:44
  • @jezrael sorry for my mistake, at first it worked but with a warning and not an error. So this answer solved my question. My question is not similar to the mentioned questions, because they are considering changing indexes and that is what I didn't want to do. Thanks again. – Ali Khatami Feb 06 '20 at 11:47