简体   繁体   中英

How to create a function for a dataframe with pandas

I have this data frame of clients purchases and I would like to create a function that gave me the total purchases for a given input of month and year.

I have a dataframe (df) with lots of columns but i'm going to use only 3 ("year", "month", "value")

This is what I'm trying but not working:

def total_purchases():
    y = input('Which year do you want to consult?')
    m = int(input('Which month do you want to consult?')
    sum = []
    if df[df['year']== y] & df[df['month']== m]:
       for i in df:
         sum = sum + df[df['value']]
    return sum

You're close, you need to ditch the IF statement and the For loop.

additionally, when dealing with multiple logical operators in pandas you need to use parenthesis to seperate the conditions.

def total_purchases(df):
    y = input('Which year do you want to consult? ')
    m = int(input('Which month do you want to consult? '))
    return df[(df['year'].eq(y)) & (df['month'].eq(m))]['value'].sum()

setup

df_p = pd.DataFrame({'year' : ['2011','2011','2012','2013'],
                  'month' : [1,2,1,2],
                  'value' : [200,500,700,900]})

Test

total_purchases(df_p)

Which year do you want to consult? 2011
Which month do you want to consult? 2

500

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM