2017-06-03 114 views
1

我有一個數據框包含返回,大小和sedols幾個日期。平鋪groupby在數據框

我的目標是確定每個日期的特定條件的頂部和底部值,即我希望每個日期的最高十分位最大尺寸條目和最低十分位尺寸最小條目並將它們標記在'xx'的新列中'和'yy'。

我很困惑如何在分組時使用平鋪以及創建新列,這裏是我已經擁有的。

import pandas as pd 
import numpy as np 
import datetime as dt 

from random import choice 
from string import ascii_uppercase 

def create_dummy_data(start_date, days, entries_pday): 
    date_sequence_lst = [dt.datetime.strptime(start_date,'%Y-%m-%d') + 
dt.timedelta(days=x) for x in range(0,days)] 
    date_sequence_lst = date_sequence_lst * entries_pday     
    returns_lst = [round(np.random.uniform(low=-0.10,high=0.20),2) for _ in range(entries_pday*days)] 

    size_lst = [round(np.random.uniform(low=10.00,high=10000.00),0) for _ in range(entries_pday*days)] 

    rdm_sedol_lst = [(''.join(choice(ascii_uppercase) for i in range(7))) for x in range(entries_pday)] 
    rdm_sedol_lst = rdm_sedol_lst * days 

    dates_returns_df = pd.DataFrame({'Date':date_sequence_lst , 'Sedols':rdm_sedol_lst, 'Returns':returns_lst,'Size':size_lst}) 
    dates_returns_df = dates_returns_df.sort_values('Date',ascending=True) 
    dates_returns_df = dates_returns_df.reset_index(drop=True) 
    return dates_returns_df 


def order_df_by(df_in,column_name): 
    df_out = df_in.sort_values(['Date',column_name],ascending=[True,False]) 
    return df_out 


def get_ntile(df_in,ntile): 
    df_in['Tiled'] = df_in.groupby(['Date'])['Size'].transform(lambda x : pd.qcut(x,ntile)) 
    return df_in 

if __name__ == "__main__": 
    # create dummy returns 
    data_df = create_dummy_data('2001-01-01',31,10) 
    # sort by attribute 
    data_sorted_df = order_df_by(data_df,'Size') 
    #ntile data per date 
    data_ntiled = get_ntile(data_sorted_df, 10) 

    for key, item in data_ntiled: 
     print(data_ntiled.get_group(key)) 

到目前爲止我會期待基於針對每個日期「尺寸」 deciled結果,下一個步驟將是分別以過濾只對等分1和等分10和標誌的條目「XX」和「YY」 。

感謝

回答

1

考慮對pandas.qcut方法,使用transform用標籤1通過NTILE + 1用於等分柱,然後有條件地設置標誌np.where使用等分值:

... 
def get_ntile(df_in, ntile): 
    df_in['Tiled'] = df_in.groupby(['Date'])['Size'].transform(lambda x: pd.qcut(x, ntile, labels=list(range(1, ntile+1)))) 
    return df_in 

if __name__ == "__main__": 
    # create dummy returns 
    data_df = create_dummy_data('2001-01-01',31,10) 
    # sort by attribute 
    data_sorted_df = order_df_by(data_df,'Size') 
    #ntile data per date 
    data_ntiled = get_ntile(data_sorted_df, 10) 

    data_ntiled['flag'] = np.where(data_ntiled['Tiled']==1.0, 'YY', 
            np.where(data_ntiled['Tiled']==10.0, 'XX', np.nan)) 

    print(data_ntiled.reset_index(drop=True).head(15)) 

#   Date Returns Sedols Size Tiled flag 
# 0 2001-01-01 -0.03 TEEADVJ 8942.0 10.0 XX 
# 1 2001-01-01 -0.03 PDBWGBJ 7142.0  9.0 nan 
# 2 2001-01-01  0.03 QNVVPIC 6995.0  8.0 nan 
# 3 2001-01-01  0.04 NTKEAKB 6871.0  7.0 nan 
# 4 2001-01-01  0.20 ZVVCLSJ 6541.0  6.0 nan 
# 5 2001-01-01  0.12 IJKXLIF 5131.0  5.0 nan 
# 6 2001-01-01  0.14 HVPDRIU 4490.0  4.0 nan 
# 7 2001-01-01 -0.08 XNOGFET 3397.0  3.0 nan 
# 8 2001-01-01 -0.06 JOARYWC 2582.0  2.0 nan 
# 9 2001-01-01  0.12 FVKBQGU 723.0  1.0 YY 
# 10 2001-01-02  0.03 ZVVCLSJ 9291.0 10.0 XX 
# 11 2001-01-02  0.14 HVPDRIU 8875.0  9.0 nan 
# 12 2001-01-02  0.08 PDBWGBJ 7496.0  8.0 nan 
# 13 2001-01-02  0.02 FVKBQGU 7307.0  7.0 nan 
# 14 2001-01-02 -0.01 QNVVPIC 7159.0  6.0 nan