对数据框中的元素进行分组并按时间顺序显示它们

问题描述 投票:0回答:1

考虑以下数据框,其中

Date
的格式为
DD-MM-YYY
:

 Date        Time      Table
 01-10-2000  13:00:03  B
 01-10-2000  13:00:04  A  
 01-10-2000  13:00:05  B
 01-10-2000  13:00:06  A
 01-10-2000  13:00:07  B
 01-10-2000  13:00:08  A

我如何1)按

Table
对观察结果进行分组,2)根据每组内的
Date
Time
对行进行排序,3)根据
Date
Time
按时间顺序显示组他们的第一次观察?

 Date        Time      Table
 01-10-2000  13:00:03  B
 01-10-2000  13:00:05  B
 01-10-2000  13:00:07  B
 01-10-2000  13:00:04  A
 01-10-2000  13:00:06  A 
 01-10-2000  13:00:08  A 
python pandas dataframe sorting group-by
1个回答
0
投票
data = {
    'Date': ['01-10-2000', '01-10-2000', '01-10-2000', '01-10-2000', '01-10-2000', '01-10-2000'],
    'Time': ['13:00:03', '13:00:04', '13:00:05', '13:00:06', '13:00:07', '13:00:08'],
    'Table': ['B', 'A', 'B', 'A', 'B', 'A']
}
df = pd.DataFrame(data)


df['DateTime'] = pd.to_datetime(df['Date'] + ' ' + df['Time'], format='%d-%m-%Y %H:%M:%S')
df_sorted = df.sort_values('DateTime')

first_occurrences = df_sorted.groupby('Table', as_index=False).first()
table_order = first_occurrences.sort_values('DateTime')['Table']

df_sorted['Table_cat'] = pd.Categorical(df_sorted['Table'], categories=table_order, ordered=True)
result = df_sorted.sort_values(['Table_cat', 'DateTime'])
result = result.reset_index(drop=True).drop('Table_cat', axis=1)
© www.soinside.com 2019 - 2024. All rights reserved.