1

I'm pretty new to python and pandas, and know only the basics. Nowadays I'm conducting a research and I need your kind help.

Let’s say I have data on births, containing 2 variables: Date and Country.

Date    Country
1.1.20  USA
1.1.20  USA
1.1.20  Italy
1.1.20  England
2.1.20  Italy
2.1.20  Italy
3.1.20  USA
3.1.20  USA

Now I want to create a third variable, let’s call him ‘Births’, which contains the number of births in country at a date. In other words, I want to stick to just one row for each date+country combination by aggregating the number of countries in each date, so I end up with something like this:

Date    Country Births
1.1.20  USA     2
1.1.20  Italy   1
1.1.20  England 1
2.1.20  Italy   2
3.1.20  USA     2

I’ve tried many things, but nothing seemed to work. Any help will be much appreciated.

Thanks, Eran

1 Answer 1

1

I guess you can use the groupby method of your DataFrame, then use the size method to count the number of individuals in each group :

df.groupby(by=['Date', 'Country']).size().reset_index(name='Births')

Output:

     Date  Country  Births
0  1.1.20  England       1
1  1.1.20    Italy       1
2  1.1.20      USA       2
3  2.1.20    Italy       2
4  3.1.20      USA       2

Also, the pandas documentation has several examples related to group-by operations : https://pandas.pydata.org/pandas-docs/stable/user_guide/groupby.html.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.