I have a .csv formatted as following:
Year Number
2001 5
2001 10
2003 15
My goal is to take a user input (year) and take the average of all the numbers that share the same year using python's pandas.
For example, If I chose the year "2001" I should get 7.5.
CodePudding user response:
I would take the input data, convert it to "int", then filter your dataframe accordingly and take the mean value from ['Number'] column.
So it would look like this:
#Preparing the data
import pandas as pd
df=pd.DataFrame()
df['Year']=pd.Series([2001,2001,2003])
df['Number']=pd.Series([5,10,15])
#Take input year from user
x=input(prompt='Please enter a year number: ')
#Filter your df by the input from customer , then select 'Number' column and calculate the mean
df[df['Year']==int(x)]['Number'].mean()
CodePudding user response:
I’m using a standard dataframe of mine.
import pandas as pd, numpy as np
df = pd.DataFrame({'Service': np.arange(8),
'Ticket': np.random.rand(8),
'Year' : np.random.randint(2010,2013,8),
'Var_1': np.random.rand(8), # values column
'Var_1_View': 'temp temp temp temp temp temp temp temp'.split(), # header of values of column
'Var_2': np.arange(8),
'Var_2_View': 'pres pres pres pres pres pres pres pres'.split(),
'Var_3': np.arange(8) * 2,
'Var_3_View': 'shift shift shift shift shift shift shift shift'.split(),
'D': np.arange(8) * 5,
'Mess_3': np.random.rand(8),
'Mess_3_View': 'id id id id id id id id'.split(),
'E': np.arange(8)})
df_extract=df.groupby('Year').mean()
df_extract.loc[int(input('Please insert Year...'))]