import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
new_series = pd.Series([7, 8,9,10], name='C')
df['B'] = new_series
in this code the column ‘B’ does not get replaced with new series. so I want the df to be
df["B"]=[1, 2, 3,nan]
df["B"]=[7, 8,9,10]
>Solution :
Make indices between df
and new_series
agreed, then – assign the needed column:
df.reindex(new_series.index).assign(B=new_series)
A B
0 1.0 7
1 2.0 8
2 3.0 9
3 NaN 10