考虑以下数据框,其中
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
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)