Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding DataFrames in pandas

I have DataFrames which have different name's but are all indexed by the same time series. Now I would like to add the values in them. So far I'm using a for loop for this.
If I use df1 + df2 I get a DataFrame with the same index but with a column for each name with all NaN values in them.
If I use df1.add(df2) (with an optional fill_value=0) I get a DataFrame with the values of the first DataFrame, which is the same result as when I use df1.combineAdd(df2).

Any hints on how to add the values except for looping over all the indexes and adding the values?

like image 228
wiseveri Avatar asked Mar 05 '26 22:03

wiseveri


1 Answers

If you know the index is the same between the two and you don't care about the column names, just do:

DataFrame(df1.values + df2.values, df1.index, df1.columns)
like image 179
Chang She Avatar answered Mar 08 '26 11:03

Chang She



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!