Pandas if/then aggregation

Viewed 119

I've been searching SO and haven't figured this out yet. Hoping someone can aide this python newb to solving my problem.

I'm trying to figure out how to write an if/then statement in python and perform an aggregation off that if/then statement. My end goal is to say if the date = 1/7/2017 then use the value in the "fake" column. If date = all else then average the two columns together.

Here is what I have so far:

import pandas as pd
import numpy as np
import datetime

np.random.seed(42)
dte=pd.date_range(start=datetime.date(2017,1,1), end= datetime.date(2017,1,15))
fake=np.random.randint(15,100, size=15)
fake2=np.random.randint(300,1000,size=15)

so_df=pd.DataFrame({'date':dte,
             'fake':fake,
             'fake2':fake2})

so_df['avg']= so_df[['fake','fake2']].mean(axis=1)
so_df.head()
4 Answers
Related