0

Find the total number of visits of the bird Cranes?

Program:

import pandas as pd
import numpy as np

df = pd.DataFrame({'birds': ['Cranes', 'Cranes', 'plovers', 'spoonbills', 'spoonbills', 'Cranes', 'plovers', 'Cranes', 'spoonbills', 'spoonbills'], 'age': [3.5, 4, 1.5, np.nan, 6, 3, 5.5, np.nan, 8, 4], 'visits': [2, 4, 3, 4, 3, 4, 2, 2, 3, 2], 'priority': ['yes', 'yes', 'no', 'yes', 'no', 'no', 'no', 'yes', 'no', 'no']})

g=df.groupby(['birds','visits']).sum()
g

OUTPUT:

        age
birds   visits  
Cranes     2    3.5
           4    7.0
plovers    2    5.5
           3    1.5
spoonbills 2    4.0
           3    14.0
           4    0.0

Desired Output:

birds   visits
Cranes    2
Cranes    4
Cranes    4
Cranes    2
Total    12

Also let me know how to remove the age column which is coming?

If i try using groupby also other columns are coming and the result is not clear

3
  • import pandas as pd import numpy as np df = pd.DataFrame({'birds': ['Cranes', 'Cranes', 'plovers', 'spoonbills', 'spoonbills', 'Cranes', 'plovers', 'Cranes', 'spoonbills', 'spoonbills'], 'age': [3.5, 4, 1.5, np.nan, 6, 3, 5.5, np.nan, 8, 4], 'visits': [2, 4, 3, 4, 3, 4, 2, 2, 3, 2], 'priority': ['yes', 'yes', 'no', 'yes', 'no', 'no', 'no', 'yes', 'no', 'no']}) g=df.groupby(['birds']).sum() g['visits'] Commented May 19, 2019 at 6:01
  • You've actually asked two questions so I've posted two links for you to read. Also, groupby is quite far from what you need to do. Commented May 19, 2019 at 6:02
  • 1
    @jezrael It smelled like homework and they seem to have no idea what they are doing. Rather than answer it I would close it and direct them to links to read. Looks like we share different opinions on the matter, unfortunately... Commented May 19, 2019 at 6:17

1 Answer 1

0

i think i got the answer. Please check anyone if it is correct or not

import pandas as pd import numpy as np

df = pd.DataFrame({'birds': ['Cranes', 'Cranes', 'plovers', 'spoonbills', 'spoonbills', 'Cranes', 'plovers', 'Cranes', 'spoonbills', 'spoonbills'], 'age': [3.5, 4, 1.5, np.nan, 6, 3, 5.5, np.nan, 8, 4], 'visits': [2, 4, 3, 4, 3, 4, 2, 2, 3, 2], 'priority': ['yes', 'yes', 'no', 'yes', 'no', 'no', 'no', 'yes', 'no', 'no']})

g=df.groupby(['birds']).sum() g['visits']

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

3 Comments

OUTPUT: birds Cranes 12 plovers 5 spoonbills 12 Name: visits, dtype: int64
If need birds visits Cranes 2 Cranes 4 Cranes 4 Cranes 2 Total 12 like mentioned in question, it is not correct.
It is to find the total no of Cranes,however i found the total no of all kinds of birds but not Cranes in particular. Please let me know how to find the sum of only Cranes row and also show if we want to create a new row Total and store the sum there.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.