Skip to content
Advertisement

Merging pandas DataFrames generated with a loop on SQL Database Data

This works BUT the outputs are not matching on the index (Date). Instead the new columns are added but start at the first dataframes last row i.e. the data is stacked “on top” of each other so the Date index is repeated. Is there a way to iterate and create columns that are matched by Date?

Output:

Thanks!

Advertisement

Answer

Just based on what your current output is and what I think your desired output is, I think you can get away with just a df.groupby('Date').sum(). Running that will group any duplicates in the ‘Date’ column and do a sum on all the values it finds for each column. If I’m understanding right, each column will only have a single value for the date-row, so it’ll ‘sum’ that single number: that is, it’ll return that number.

I copied the little output section you have above (and removed the blank rows) and just did df.groupby('Date').sum() and got this:

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement