Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

import pandas as pd

Comment

When you update a DataFrame or a Series using the method DataFrame.update, the updating is done by matching index and (columns) names. It is the same when you update a whole column of a DataFrame by assigning a new Series to it. This might not what you want. If you want to update a DataFrame or a Series by matching position, avoid putting the new data into an object (e.g., DataFrame or Series) with index. For example, you can assign a scalar of a list to a whole column of a DataFrame and the update is done by matching position.

s1 = pd.Series([1, 2, 3, 4])
s2 = pd.Series([100, 200, 300, 400], index=[0, 2, 4, 6])
print(s1)
print(s2)
0    1
1    2
2    3
3    4
dtype: int64
0    100
2    200
4    300
6    400
dtype: int64
s1.update(s2)
s1
0 100 1 2 2 200 3 4 dtype: int64
df1 = pd.DataFrame({"x": [1, 2, 3, 4, 5], "y": [5, 4, 3, 2, 1]})
print(df1.head())

df2 = pd.DataFrame(
    {"x0": [1, 2, 3, 4, 5], "y": [50, 40, 30, 20, 10]}, index=[0, 1, 2, 300, 400]
)
df2.head()
   x  y
0  1  5
1  2  4
2  3  3
3  4  2
4  5  1
Loading...
df1.update(df2)
df1
Loading...
df1 = pd.DataFrame({"x": [1, 2, 3, 4, 5], "y": [5, 4, 3, 2, 1]})
df1.head()
Loading...
df2 = pd.DataFrame(
    {"x": [1, 2, 3, 4, 5], "y": [50, 40, 30, 20, 10]},
    index=["r0", "r1", "r2", "r3", "r4"],
)
df2.head()
Loading...
df1.y = df2.y
df1
Loading...
df1.y = df2.y.values
df1
Loading...