2017-03-15 60 views
1

我想獲得最新的專欄,當列III是相同的。 所以結果應該是。 1/30/2017擊敗了1/27/2017如何通過某些列的最大日期獲取行?

I I III     IV 
A X 1/30/2017 9:33:00 AM some_data 
A Y 1/30/2017 9:33:00 AM some_data 
A Z 1/30/2017 9:33:00 AM some_data 
A X 1/27/2017 4:53:00 PM some_data 
A Y 1/27/2017 4:53:00 PM some_data 
A Z 1/27/2017 4:53:00 PM some_data 
B X 1/30/2017 9:33:00 AM some_data 
B Y 1/30/2017 9:33:00 AM some_data 
B Z 1/30/2017 9:33:00 AM some_data 
B X 1/27/2017 4:53:00 PM some_data 
B Y 1/27/2017 4:53:00 PM some_data 
B Z 1/27/2017 4:53:00 PM some_data 

這是我想要的結果。

I I III     IV 
A X 1/30/2017 9:33:00 AM some_data 
A Y 1/30/2017 9:33:00 AM some_data 
A Z 1/30/2017 9:33:00 AM some_data 
B X 1/30/2017 9:33:00 AM some_data 
B Y 1/30/2017 9:33:00 AM some_data 
B Z 1/30/2017 9:33:00 AM some_data 

有人可以幫我弄清楚如何提取這些行嗎?

+0

可以顯示一些代碼,你試過。 – Rednivrug

回答

0

看起來你想要的是一個groupby()transform()max()

代碼:

data = [ 
    ('I', 'II', 'III', 'IV'), 
    ('A', 'X', '1/30/2017 9:33:00 AM', 'some_data'), 
    ('A', 'Y', '1/30/2017 9:33:00 AM', 'some_data'), 
    ('A', 'Z', '1/30/2017 9:33:00 AM', 'some_data'), 
    ('A', 'X', '1/27/2017 4:53:00 PM', 'some_data'), 
    ('A', 'Y', '1/27/2017 4:53:00 PM', 'some_data'), 
    ('A', 'Z', '1/27/2017 4:53:00 PM', 'some_data'), 
    ('B', 'X', '1/30/2017 9:33:00 AM', 'some_data'), 
    ('B', 'Y', '1/30/2017 9:33:00 AM', 'some_data'), 
    ('B', 'Z', '1/30/2017 9:33:00 AM', 'some_data'), 
    ('B', 'X', '1/27/2017 4:53:00 PM', 'some_data'), 
    ('B', 'Y', '1/27/2017 4:53:00 PM', 'some_data'), 
    ('B', 'Z', '1/27/2017 4:53:00 PM', 'some_data'), 
] 

import pandas as pd 
df = pd.DataFrame(data[1:], columns=data[0]) 
df['III'] = pd.to_datetime(df['III']) 

# groupby first two columns, then get the maximum value in the third column 
idx = df.groupby(['I', 'II'])['III'].transform(max) == df['III'] 

# use the index to fetch correct rows in dataframe 
print(df[idx]) 

結果:

I II     III   IV 
0 A X 2017-01-30 09:33:00 some_data 
1 A Y 2017-01-30 09:33:00 some_data 
2 A Z 2017-01-30 09:33:00 some_data 
6 B X 2017-01-30 09:33:00 some_data 
7 B Y 2017-01-30 09:33:00 some_data 
8 B Z 2017-01-30 09:33:00 some_data 
相關問題