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
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.
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 .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With