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
:
- First use the zip function to bind the ix column to the two b columns
- 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
- Finally, the updated dictionary is turned into a column and returned to the dataframe. The
above method is easier to understand. There are two methods for the dataframe. The first is:
1. Reconstruct the index first
2. Use apply () to build the lambda function to create a separate column
3. Finally, give this column to df1
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