def annual_population(in_df, out_df, o_path): in_df = calls_cleanup(in_df) out_df = calls_cleanup(out_df) df = in_df.append(out_df, sort=False) df = df.groupby(['MOUHAFAZA', 'YEAR', 'KAZA'], as_index=False).agg({'NB_CALLS': 'sum'}) df = df.apply(lambda row: get_population(df, row), axis=1) save(df, o_path)
def structure(df): df2=pd.DataFrame() for year in np.unique(np.array(df['YEAR'])): sub=df[df['YEAR'] == year] for age in np.unique(np.array(sub['AGE_GROUP'])): m=-1*sub[(sub['YEAR']==year)&(sub['AGE_GROUP']==age)&(sub['GENDER']=='M')]['NB_CALLS'].values.item() f=sub[(sub['YEAR']==year)&(sub['AGE_GROUP']==age)&(sub['GENDER']=='F')]['NB_CALLS'].values.item() df2=df2.append({'YEAR':year,'AGE_GROUP':age,'MALE':m,'FEMALE':f},ignore_index=True) save(df2,'pop_pyramid.csv')
def sites_per_kaza(df, o_path): o = pd.DataFrame() kazas = np.unique(df['KAZA']) for year in years: for kaza in kazas: sub = df[(df['KAZA'] == kaza) & (df['YEAR'] == year)] o = o.append({ 'YEAR': year, 'KAZA': kaza, 'NB_SITES': len(sub) }, ignore_index=True) save(o, o)
def mb(df, o_path): df = df.groupby(['MOUHAFAZA', 'YEAR', 'KAZA'], as_index=False).agg({ 'UPLINK_MB': 'sum', 'DOWNLINK_MB': 'sum', 'TOTAL_MB': 'sum', }) df['ALFA_UP/DOWN'] = df['ALFA_UPLINK_MB'].astype( float) / df['ALFA_DOWNLINK_MB'].astype(float) df['ALFA_UP/TOTAL'] = df['ALFA_UPLINK_MB'].astype( float) / df['ALFA_TOTAL_MB'].astype(float) df['ALFA_DOWN/TOTAL'] = df['ALFA_DOWNLINK_MB'].astype( float) / df['ALFA_TOTAL_MB'].astype(float) save(df, o_path)
def indicators(): df = read_output('daily.csv') df2 = pd.DataFrame() for year in np.unique(df['YEAR']): sub = df[df['YEAR'] == year] ramadan = sub[sub['RAMADAN']]['NB_CALLS'].mean() / sub[ sub['RAMADAN'] == False]['NB_CALLS'].mean() weekend = sub[sub['TYPE'] == 'WEEKEND']['NB_CALLS'].mean() / sub[ sub['TYPE'] == 'WEEKDAY']['NB_CALLS'].mean() holiday = sub[sub['HOLIDAY']]['NB_CALLS'].mean() / sub[ sub['HOLIDAY'] == False]['NB_CALLS'].mean() df2 = df2.append( { 'YEAR': year, 'RAMADAN_RATIO': ramadan, 'HOLIDAY_RATIO': holiday, 'WEEKEND_RATIO': weekend }, ignore_index=True) save(df2, 'refugee_activity.csv')
def economics(in_df, out_df, o_path): in_to_out = pd.DataFrame() for _, row in in_df.iterrows(): dic = {} dic['YEAR'] = row['YEAR'] dic['MOUHAFAZA'] = row['MOUHAFAZA'] dic['KAZA'] = row['KAZA'] out = out_df[(out_df['KAZA'] == row['KAZA']) & (out_df['YEAR'] == row['YEAR'])] dic['NB_RATIO'] = out['NB_CALLS'].values.item() / row['NB_CALLS'] dic['DURATION_RATIO'] = out['DURATION_IN_SEC'].values.item( ) / row['DURATION_IN_SEC'] dic['AVG_IN_DURATION_SEC'] = row['DURATION_IN_SEC'] / row['NB_CALLS'] dic['AVG_IN_DURATION_MIN'] = row['DURATION_IN_MIN'] / row['NB_CALLS'] dic['AVG_OUT_DURATION_SEC'] = out['DURATION_IN_SEC'].values.item( ) / out['NB_CALLS'].values.item() dic['AVG_OUT_DURATION_MIN'] = out['DURATION_IN_MIN'].values.item( ) / out['NB_CALLS'].values.item() in_to_out = in_to_out.append(dic, ignore_index=True) save(in_to_out, o_path)
def set_weekday(): df = read('daily.csv') df = df.apply(lambda row: weekday(row), axis=1) save(df, 'daily.csv')
def annual_mobility(): mob = read_output('population.csv') mob = mob.apply(lambda row: get_variation(mob, row), axis=1) save(mob, 'mobility.csv')