Home > Net >  how do I perform the following operation in python dataframe
how do I perform the following operation in python dataframe

Time:05-17

below are my two dfs

df = pd.DataFrame(np.random.randint(1,10,(5,3)),columns=['a','b','c'])
dd = pd.DataFrame(np.repeat(3,2),columns=['a'])

I want to replace the column 'a' of df with values in column 'a' of dd. Any empty rows are replaced by zero "only" for column 'a'. All other columns of df remain unchanged.

so column 'a' should contain 3,3,0,0,0

CodePudding user response:

This is probably not the cleanest way, but it works.

    df['a'] = dd['a']
    df['a'] = df['a'].fillna(0)
  • Related