-1

I would like to replace the values in the column(charge) of Data 'B' into the column(charge) of Data 'A' by comparing the two dataFrames.

Example:

data A:                          data B:

Codes  | charge                Codes  | charge
-----------------            ---------------------
Abc123    100                  Abc123    50
Abc345    75                   Abc345    75
Abc645    0                    Abc645    0
Abc456    200                  Abc456    200
Abc789    123
::  ::    ::
::  ::    ::

So on and so forth

Data 'B' has predetermined values for those codes. Please, can someone help me do this in python.

martineau
  • 119,623
  • 25
  • 170
  • 301
Pavan
  • 1
  • 2
  • Could you rephrase and reformat your question? It's unclear what you want. Edit: nevermind, you reposted this question after it was already marked as duplicate. – Mike Dec 11 '19 at 15:44
  • Mike, it was not resolved actually – Pavan Dec 11 '19 at 15:46
  • Does this answer your question? [Pandas Merging 101](https://stackoverflow.com/questions/53645882/pandas-merging-101) – Mike Dec 11 '19 at 15:46
  • I have 2 dataFrames, Df1 and Df2. Df1 has a fields 'codes' and 'Charges'. These charges values are random. For that reason, i have created Df2 with pre-determined values for those codes. So match the codes from df1 and df2, i would replace the charges in df1 by df2. – Pavan Dec 11 '19 at 15:50
  • Take a look at the linked question. – Mike Dec 11 '19 at 15:54

1 Answers1

0

You could to something like this:

for (df1_index, df1_row), (df2_index, df2_row) in zip(df1.iterrows(), df2.iterrows()):
    df1.at[df1_index, 'codes'] = df2_row['whatever']
    df1.at[df1_index, 'Charges'] = df2_row['whatever']

You can add if-statements as well inside the loop depending on how and when you like to join the DataFrames. But this is essentially how to loop through two DataFrames at a time.

Tinu
  • 2,432
  • 2
  • 8
  • 20