How To Add Multiple Different Dataframe Using For Loop?
I have multiple data frames and I want to add the values of the next data frame after the 3rd value of the previous data frame. I am very new with pyhton and I am using google cola
Solution 1:
Use pd.merge
df = df1.merge(df2.assign(Index=df2['Index']+3), how='outer') \
.merge(df3.assign(Index=df3['Index']+6), how='outer')
df['Column_2'] += df['Column_1'].fillna(0)
df['Column_3'] += df['Column_2'].fillna(0)
>>> df
Index Column_1 Column_2 Column_3
001.0NaNNaN111.0NaNNaN221.0NaNNaN331.03.0NaN441.03.0NaN551.03.0NaN66NaN2.05.077NaN2.05.088NaN2.05.099NaNNaN3.01010NaNNaN3.01111NaNNaN3.0
Update
Is there a way I can render the result in one column only?
df['Column_4'] = df.ffill(axis=1)['Column_3']
>>> df
Index Column_1 Column_2 Column_3 Column_4
001.0NaNNaN1.0111.0NaNNaN1.0221.0NaNNaN1.0331.03.0NaN3.0441.03.0NaN3.0551.03.0NaN3.066NaN2.05.05.077NaN2.05.05.088NaN2.05.05.099NaNNaN3.03.01010NaNNaN3.03.01111NaNNaN3.03.0
Post a Comment for "How To Add Multiple Different Dataframe Using For Loop?"