How to update a column of one dataframe to another dataframe

When you want to update the data of another dataframe from a column in a dataframe, and there is a mapping column index, this piece of python is not like sql. It can be easily related to join and then update through join (maybe by introducing the sql module to python achieve).
At present, in python, the relatively simple way is to bind the common column to the updated column and the updated column through the zip function, and then use the update function in the dictionary to solve it.
ex:
Create df1 and df2 separately, and now we need to update the b column in df1 through the shared ix column in df2
Insert picture description here
:

  1. First use the zip function to bind the ix column to the two b columns
    Insert picture description here
  2. Put the updated dict in an empty dictionary first, and then use the update function to replace the dict to be updated with the corresponding value of the dictionary
    Insert picture description here
  3. Finally, the updated dictionary is turned into a column and returned to the dataframe. The
    Insert picture description here
    above method is easier to understand. There are two methods for the dataframe. The first is:
    1. Reconstruct the index first
    Insert picture description here

2. Use apply () to build the lambda function to create a separate column
Insert picture description here
3. Finally, give this column to df1

Insert picture description here

The second method is to first match df1 and df2 according to the ix column, and then use the fillna () function to fill the replaced column to the NaN in the replaced column

Published 69 original articles · praised 11 · 20,000+ views

Guess you like

Origin blog.csdn.net/weixin_41636030/article/details/104402224
Recommended