2017-06-23 54 views
1

我有類似下面這樣的數據集:子選擇多指數大熊貓數據幀來創建多個子集(使用字典)

df_lenght = 240 
df = pd.DataFrame(np.random.randn(df_lenght,2), columns=['a','b']) 
df['datetime'] = pd.date_range('23/06/2017', periods=df_lenght, freq='H') 
unique_jobs = ['job1','job2','job3',] 
job_id = [unique_jobs for i in range (1, int((df_lenght/len(unique_jobs))+1) ,1) ] 
df['job_id'] = sorted([val for sublist in job_id for val in sublist]) 
df.set_index(['job_id','datetime'], append=True, inplace=True) 

print(df[:5])回報:

         a   b 
    job_id datetime        
0 job1 2017-06-23 00:00:00 -0.067011 -0.516382 
1 job1 2017-06-23 01:00:00 -0.174199 0.068693 
2 job1 2017-06-23 02:00:00 -1.227568 -0.103878 
3 job1 2017-06-23 03:00:00 -0.847565 -0.345161 
4 job1 2017-06-23 04:00:00 0.028852 3.111738 

我怎樣才能創建多個dataframes,每個值爲job_id?那些喂到字典中的人可以輕鬆檢索嗎? 感謝

回答

1

你可以解開一個groupby對象到詞典:

dfs = {job: df for job, df in df.groupby(level='job_id')} 
+0

感謝。我將如何去選擇使用字典的數據呢? – Andreuccio

+1

例如,您可以簡單地編寫dfs ['job1']。 –