In [1]:
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.
In [3]:
s1 = pd.Series([1, 2, 3, 4])
s2 = pd.Series([100, 200, 300, 400], index=[0, 2, 4, 6])
print(s1)
print(s2)
In [4]:
s1.update(s2)
s1
Out[4]:
In [5]:
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()
Out[5]:
In [6]:
df1.update(df2)
df1
Out[6]:
In [50]:
df1 = pd.DataFrame({"x": [1, 2, 3, 4, 5], "y": [5, 4, 3, 2, 1]})
df1.head()
Out[50]:
In [51]:
df2 = pd.DataFrame(
{"x": [1, 2, 3, 4, 5], "y": [50, 40, 30, 20, 10]},
index=["r0", "r1", "r2", "r3", "r4"],
)
df2.head()
Out[51]:
In [53]:
df1.y = df2.y
df1
Out[53]:
In [54]:
df1.y = df2.y.values
df1
Out[54]:
In [ ]: