Python学习笔记--DataFrame使用

def defr_function(date_this):
    dd={'company_id':[13484491,13484491,25885969,33333333],
        'signal_code':['r_1005050000','r_1005050000','r_1005050000','r_1005050000'],
        'signal_value':[12,9,8888,1111],
        'col_formula':['净利润','净利润','净利润','净利润'],
        'col_value':[12,9,8888,1111],
        'update_date':['2015-04-03','2015-04-03','2015-04-03','2015-04-03'],
        'report_date':['2015-03-31','2014-09-30','2015-03-31','2015-03-31']}
    bank_raw_all = pd.DataFrame(dd)
    bank_raw_all['report_date']=pd.to_datetime(bank_raw_all['report_date'])
    bank_raw_all['update_date']=pd.to_datetime(bank_raw_all['update_date'])

    data_latest = bank_raw_all[(bank_raw_all['report_date'] > mbef(4, date_this)) & (bank_raw_all['report_date'] <= date_this)]
    data_latest = data_latest.merge(pd.DataFrame(data_latest.groupby('company_id').max().report_date).reset_index(), on=['company_id','report_date'], how='inner')
    data_latest = data_latest.rename(columns={'report_date':'report_date_last'})
    data = bank_raw_all.merge(data_latest, on='company_id')
    data = data.assign(date_1y_ago=map(lambda x: datetime.date(x.year-1,x.month,x.day), data['report_date_last']))
    data_final = data[data['report_date'] > pd.to_datetime(data['date_1y_ago'])]
    result = data_final.groupby('company_id').apply(lambda x: norm.ppf(0.25, x['signal_value_x'].mean(),x['signal_value_x'].std()))
    print  result
    result = pd.DataFrame(result.reset_index())
    result.columns = ['company_id','value']
    NaNData = result[np.isnan(result['value'])]
    MergeData = data_final.merge(NaNData)
    print  MergeData
    df_MergeData = result.merge(MergeData)
    df = df_MergeData[['company_id','signal_value_x']]
    print  df
    result = result.merge(df,how='outer')
    result = result.fillna(0)
    result['value'] = result['value']+result['signal_value_x']
    result = result.drop(['signal_value_x'],axis=1)
    print result
    return

你可能感兴趣的:(Python)