简体   繁体   中英

How to update the value of one column dF after matching the rows of from another column from different dataframe

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM