0

I have a dataframe like this: [2]: https://i.stack.imgur.com/D2buG.png

I want to merge the same day's readings then plus them and add a new column as day_total_read, how can i do that ?

For example indexs 2-3, 8-9, 10-11

  • You could use [groupby](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.groupby.html) – Daneolog Jun 23 '20 at 15:12

1 Answers1

0

I'm not particularly certain how you're going to have a new column in the current table, since you'll be having an aggregate over a couple rows, and it won't be a one-to-one mapping of rows. Overall, though, you're pretty much describing a groupby operation.

Assuming df is your dataframe, df.groupby("date").sum() should do the trick just fine.

This'll return a new dataframe though, so if you want to merge it back into the original dataframe as a new column (like I said, won't be one-to-one, so there will be duplicates here) you'd probably wanna just column-wise append, or something like that

Here's an example of it at work

Daneolog
  • 286
  • 1
  • 2
  • 13
  • It worked and duplicated as you said, is there a way to get rid of duplicates and show two same row as one ? –  Jun 23 '20 at 16:03
  • I've added a repl link here, not really sure what you're saying that there are duplicates. If you just purely use the command, you should be seeing the rows grouped as one – Daneolog Jun 23 '20 at 16:11
  • For no duplicates, you can use the `new_df` version, and if you want to merge it back, it's `merged_df` – Daneolog Jun 23 '20 at 16:22