Compare columns of pandas dataframes and fill missing values -
i have 2 pandas dataframes follows.
list1 = [{'salt': 0.2, 'fat': 0.8}, {'fat': 1.0, 'protein': 0.9}] df1 = pd.dataframe(line2) # fill missing values zeros df1.fillna(0, inplace=true) list2 = [{'salt': 0.1, 'sugar': 0.9}, {'oil': 0.9, 'sugar': 0.8, 'salt': 0.2}, {'protein': 0.9}] df2 = pd.dataframe(line2) # fill missing values zeros df2.fillna(0, inplace=true) my 2 data frames follows.
df1: fat protein salt 0 0.8 0.0 0.2 1 1.0 0.9 0.0 df2: oil protein salt sugar 0 0.0 0.0 0.1 0.9 1 0.9 0.0 0.2 0.8 2 0.0 0.9 0.0 0.0 now want compare df1 , df2 find missing topics , fill them zero, final version of dataframes looks follows.
df1: fat protein salt oil sugar 0 0.8 0.0 0.2 0 0 1 1.0 0.9 0.0 0 0 df2: oil protein salt sugar fat 0 0.0 0.0 0.1 0.9 0 1 0.9 0.0 0.2 0.8 0 2 0.0 0.9 0.0 0.0 0 i know within dataframe using df1.fillna(0, inplace=true). 2 dataframes, how can it?
use pd.dataframe.align making sure align along column axis. use argument fill_value=0 fill in missing elements zero.
df1, df2 = df1.align(df2, axis=1, fill_value=0) df1 fat oil protein salt sugar 0 0.8 0 0.0 0.2 0 1 1.0 0 0.9 0.0 0 df2 fat oil protein salt sugar 0 0 0.0 0.0 0.1 0.9 1 0 0.9 0.0 0.2 0.8 2 0 0.0 0.9 0.0 0.0
Comments
Post a Comment