Home > Back-end >  Merging data in Pandas dataframe by values in a column
Merging data in Pandas dataframe by values in a column

Time:04-02

I have a pandas dataframe like this:

Name    Year    Sales
Ann     2010    500
Ann     2011    500
Bob     2010    400
Bob     2011    700
Ed      2010    300
Ed      2011    300

I want to be able to combine the figures in the sales column for each name returning:

Name    Sales
Ann     1000
Bob     1100
Ed      600

Perhaps I need a for loop to go through and combine the 2 values for both years and create a new column, but I'm not quite sure. Is there a pandas function that can help me with this?

CodePudding user response:

That's a simple dataframe groupby.

In that case you'll just have to select the two columns you need

df = df[["Name", "Sales"]]

And then apply the groupby

df.groupby(["name"], as_index=False).sum()

By default the groupby will make the grouped by columns part of the index. If you want to keep them as colum you need to specify as_index=False

  • Related