Ben Chuanlong Du's Blog

It is never too late to learn.

Update a pandas DataFrame

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

Comments