I would like to replace an entire column on a Pandas DataFrame with another column taken from another DataFrame, an example will clarify what I am looking for
import pandas as pd
dic = {'A': [1, 4, 1, 4], 'B': [9, 2, 5, 3], 'C': [0, 0, 5, 3]}
df = pd.DataFrame(dic)
df is
'A' 'B' 'C'
1 9 0
4 2 0
1 5 5
4 3 3
Now I have another dataframe called df1
with a column "E"
that is
df1['E'] = [ 4, 4, 4, 0]
and I would like to replace column "B"
of df with column "E"
of df1
'A' 'E' 'C'
1 4 0
4 4 0
1 4 5
4 0 3
I tried to use the .replace()
method in many ways but I didn't get anything good. Can you help me?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…