I'm trying to calculate how many times a value occurs in a specific location inside a data frame.
As an example I use this data frame:
import pandas as pd
d = {'Fruit': ['Apple', 'Apple', 'Apple', 'Onion', 'Onion', 'Onion', 'Onion', 'Pear', 'Pear', 'Pear',
'Pear', 'Pear'],
'Country': ['USA', 'SUI', 'USA', 'SUI', 'USA', 'SUI', 'SUI', 'USA', 'USA', 'USA', 'SUI', 'SUI']}
df = pd.DataFrame(data=d)
I do not understand how I can calculate for example how many Apples there are in USA and SUI and add this to a 'Count' column.
The output should look something like this:
import pandas as pd
d = {'Fruit': ['Apple', 'Apple', 'Apple', 'Onion', 'Onion', 'Onion', 'Onion', 'Pear', 'Pear', 'Pear', 'Pear', 'Pear'],
'Country': ['USA', 'SUI', 'USA', 'SUI', 'USA', 'SUI', 'SUI', 'USA', 'USA', 'USA', 'SUI', 'SUI'],
'Count': [2, 1, 2, 3, 1, 3, 3, 3, 3, 3, 2, 2]}
df = pd.DataFrame(data=d)
I would know how to count the values themselves (how many apples occur in the Fruit column) but not how to add this condition to the calculation.
Thanks for your help in advance.
Try Groupby transform :
df['counts'] = df.groupby(['Fruit', 'Country'])['Country'].transform('size')
df
:
Fruit Country counts
0 Apple USA 2
1 Apple SUI 1
2 Apple USA 2
3 Onion SUI 3
4 Onion USA 1
5 Onion SUI 3
6 Onion SUI 3
7 Pear USA 3
8 Pear USA 3
9 Pear USA 3
10 Pear SUI 2
11 Pear SUI 2
You can use groupby
followed by a join
, like this:
fruit_counts = df.groupby(["Fruit", "Country"]).size().rename("Count")
df.join(fruit_counts, on=["Fruit", "Country"])
Output:
Fruit Country Count
0 Apple USA 2
1 Apple SUI 1
2 Apple USA 2
3 Onion SUI 3
4 Onion USA 1
5 Onion SUI 3
6 Onion SUI 3
7 Pear USA 3
8 Pear USA 3
9 Pear USA 3
10 Pear SUI 2
11 Pear SUI 2
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.