2017-03-06 78 views
1

我想每半年將數據拆分爲一半。因此,在我的樣本數據中,我需要將結果分成兩個獨立的數據框,其中一個是每年的前50%,另一半是另一個。另外的條件是,50%需要基於列'LG'。根據日期將數據拆分爲一半

任何人都可以幫助我嗎?

樣本數據:

import pandas as pd 
import numpy as np 

df = pd.DataFrame(
    {'LG' : ('AR1', 'AR1', 'AR1', 'AR1', 'AR1', 'AR1', 'PO1', 'PO1', 'AR1', 'AR1', 'PO1', 'PO1'), 
    'Date': ('2011-1-1', '2011-3-1', '2011-4-1', '2011-2-1', '2012-1-1', '2012-2-1', '2012-1-1', '2012-2-1', '2013-1-1', '2013-2-1', '2013-1-1', '2013-2-1'), 
    'Year': (2011, 2011, 2011, 2011, 2012, 2012, 2012, 2012, 2013, 2013, 2013, 2013)}) 

pd.to_datetime(df['Date']) 

DF:

  Date LG Year 
0 2011-01-01 AR1 2011 
1 2011-03-01 AR1 2011 
2 2011-04-01 AR1 2011 
3 2011-02-01 AR1 2011 
4 2012-01-01 AR1 2012 
5 2012-02-01 AR1 2012 
6 2012-01-01 PO1 2012 
7 2012-02-01 PO1 2012 
8 2013-01-01 AR1 2013 
9 2013-02-01 AR1 2013 
10 2013-01-01 PO1 2013 
11 2013-02-01 PO1 2013 
+3

'DF [ '日期']申請(pd.to_datetime) '是說'pd.to_datetime(df ['Date'])''的一種緩慢方式。 –

+0

編輯你每個評論 – Zanshin

回答

1

拆分中半幀上YearLG分組後。基本思路是在屬於該組的大小小於50%組找到位置

代碼:

# group by 'Year' and 'LG' 
idx = ['Year', 'LG'] 

# build a grouper 
group_by = df.groupby(idx, as_index=False) 

# need frame to re-expand the group size 
df1 = df.set_index(idx) 
df1['g_size'] = group_by.size() 

# find the rows in the top half of respective group 
top_half = (group_by.cumcount()/df1.g_size.values).values < 0.5 

# build new data frames 
top = df.loc[top_half] 
bot = df.loc[~top_half] 

代碼,以便在日期排序:

如果幀需要在分割之前按日期排序,但不希望該分類位於原始數據框中...

# group by 'Year' and 'LG' 
idx = ['Year', 'LG'] 

# sort by date 
df1 = df.sort('Date') 

# build a grouper 
group_by = df1.groupby(idx, as_index=False) 

# Need to set the index to match the result of groupby.size() 
df1 = df1.set_index(idx) 
df1['g_size'] = group_by.size() 

# find the rows in the top half of respective group 
top_half = (group_by.cumcount()/df1.g_size.values).values < 0.5 

# build new data frames 
top = df1.loc[top_half].drop('g_size', axis=1).reset_index() 
bot = df1.loc[~top_half].drop('g_size', axis=1).reset_index() 

測試代碼:

print(df) 
print('-- top') 
print(top) 
print('-- bot') 
print(bot) 
print('--') 

排序結果:

 Date LG Year 
0 2011-1-1 AR1 2011 
1 2011-3-1 AR1 2011 
2 2011-4-1 AR1 2011 
3 2011-2-1 AR1 2011 
4 2012-1-1 AR1 2012 
5 2012-2-1 AR1 2012 
6 2012-1-1 PO1 2012 
7 2012-2-1 PO1 2012 
8 2013-1-1 AR1 2013 
9 2013-2-1 AR1 2013 
10 2013-1-1 PO1 2013 
11 2013-2-1 PO1 2013 
-- top 
    Year LG  Date 
0 2011 AR1 2011-1-1 
1 2011 AR1 2011-2-1 
2 2012 AR1 2012-1-1 
3 2012 PO1 2012-1-1 
4 2013 AR1 2013-1-1 
5 2013 PO1 2013-1-1 
-- bot 
    Year LG  Date 
0 2011 AR1 2011-3-1 
1 2011 AR1 2011-4-1 
2 2012 AR1 2012-2-1 
3 2012 PO1 2012-2-1 
4 2013 AR1 2013-2-1 
5 2013 PO1 2013-2-1 

測試數據:

df = pd.DataFrame({ 
    'LG': ('AR1', 'AR1', 'AR1', 'AR1', 'AR1', 'AR1', 
      'PO1', 'PO1', 'AR1', 'AR1', 'PO1', 'PO1'), 
    'Date': ('2011-1-1', '2011-3-1', '2011-4-1', '2011-2-1', '2012-1-1', 
      '2012-2-1', '2012-1-1', '2012-2-1', '2013-1-1', '2013-2-1', 
      '2013-1-1', '2013-2-1'), 
    'Year': (2011, 2011, 2011, 2011, 2012, 2012, 2012, 2012, 2013, 
      2013, 2013, 2013) 
}) 
pd.to_datetime(df['Date']) 
+0

謝謝,但一個問題。 2011年的AR1沒有正確分割。 2011-2-1在小組'最後'和2011-3-1小組'最後'。怎麼來的? – Zanshin

+0

哦,你想分類嗎?你沒有顯示任何示例輸出,所以我沒有假設。讓我鞭打一番... –

+0

是的,我的壞。我的意思是在日期中的前50%,而不是如圖所示。感謝 – Zanshin