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

sekky
  • 834
  • 7
  • 14
Koustav
  • 1
  • 2
  • 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'] – Koustav May 19 '19 at 06: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. – cs95 May 19 '19 at 06: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... – cs95 May 19 '19 at 06:17

1 Answers1

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']

Community
  • 1
  • 1
Koustav
  • 1
  • 2
  • OUTPUT: birds Cranes 12 plovers 5 spoonbills 12 Name: visits, dtype: int64 – Koustav May 19 '19 at 06:04
  • If need `birds visits Cranes 2 Cranes 4 Cranes 4 Cranes 2 Total 12` like mentioned in question, it is not correct. – jezrael May 19 '19 at 06:09
  • 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. – Koustav May 19 '19 at 06:25