Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge rows of a dataframe in pandas based on a column

Tags:

python

pandas

I am new to pandas. I have a dataframe that looks like this

sitename            name        date               count
0  chess.com  Autobiographer  2012-05-01               2
1  chess.com  Autobiographer  2012-05-05               1
2  chess.com  Autobiographer  2012-05-15               1
3  chess.com  Autobiographer  2012-05-01               1
4  chess.com  Autobiographer  2012-05-15               1
5  chess.com  Autobiographer  2012-05-01               1

How to merge the rows based on date and sum up the count for the same date. Like in sql

select sitename, name, date count(*) from table group by date
like image 757
user3527975 Avatar asked May 28 '14 18:05

user3527975


People also ask

How do I merge rows in pandas DataFrame?

We can use the concat function in pandas to append either columns or rows from one DataFrame to another. Let's grab two subsets of our data to see how this works. When we concatenate DataFrames, we need to specify the axis. axis=0 tells pandas to stack the second DataFrame UNDER the first one.

How do I merge DataFrame by columns?

Merge Default Pandas DataFrame Without Any Key Column You can pass two DataFrame to be merged to the pandas. merge() method. This collects all common columns in both DataFrames and replaces each common column in both DataFrame with a single one. It merges the DataFrames df and df1 assigns to merged_df .


Video Answer


1 Answers

If you want to keep your sitename and name in your dataframe, you can do :

df = dataframe.groupby(['date', 'sitename', 'name']).sum()

EDIT : See @DSM's comment to reset the indexes and have a non indexed dataframe.

like image 82
TimmyCarbone Avatar answered Oct 19 '22 12:10

TimmyCarbone