Skip to content
Advertisement

how do I perform the following operation in python dataframe

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

Advertisement

Answer

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

    df['a'] = dd['a']
    df['a'] = df['a'].fillna(0)
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement