我有两个使用pandas和列标签的数据表:。
import pandas as pd
ne = pd.DataFrame({"NE_Name": ["A", "A", "A", "D", "D", "B", "B", "B", "C", "C", "C", "C"],
"NE_Unit": ["A1", "A2", "A3", "D2", "D3", "B1", "B2", "B3", "C1", "C2", "C3", "C4"]})
df = pd.DataFrame({"NE_Name": ["A", "A", "A", "D", "D", "B", "B", "A", "A", "A", "A"],
"NE_Unit": ["A1", "A2", "A3", "D2", "D3", "B1", "B3", "A1", "A2", "A3", "A2"],
"Event_Time": ["2017/2/1 5:55:51",
"2017/2/1 5:55:52",
"2017/2/1 5:55:54",
"2017/2/1 6:05:30",
"2017/2/1 6:05:30",
"2017/2/1 7:10:30",
"2017/2/1 7:10:30",
"2017/2/1 7:24:11",
"2017/2/1 7:24:21",
"2017/2/1 7:24:11",
"2017/2/1 7:55:21"],
"Clear_Time": ["2017/2/1 5:58:38",
"2017/2/1 5:58:48",
"2017/2/1 5:58:38",
"2017/2/1 7:02:06",
"2017/2/1 7:02:06",
"2017/2/1 7:18:36",
"2017/2/1 7:18:16",
"2017/2/1 7:53:37",
"2017/2/1 7:53:37",
"2017/2/1 7:53:37",
"2017/2/1 7:59:55"]})
df
Clear_Time Event_Time NE_Name NE_Unit
0 2017/2/1 5:58:38 2017/2/1 5:55:51 A A1
1 2017/2/1 5:58:48 2017/2/1 5:55:52 A A2
2 2017/2/1 5:58:38 2017/2/1 5:55:54 A A3
3 2017/2/1 7:02:06 2017/2/1 6:05:30 D D2
4 2017/2/1 7:02:06 2017/2/1 6:05:30 D D3
5 2017/2/1 7:18:36 2017/2/1 7:10:30 B B1
6 2017/2/1 7:18:16 2017/2/1 7:10:30 B B3
7 2017/2/1 7:53:37 2017/2/1 7:24:11 A A1
8 2017/2/1 7:53:37 2017/2/1 7:24:21 A A2
9 2017/2/1 7:53:37 2017/2/1 7:24:11 A A3
10 2017/2/1 7:59:55 2017/2/1 7:55:21 A A2
NE
NE_Name NE_Unit
0 A A1
1 A A2
2 A A3
3 D D2
4 D D3
5 B B1
6 B B2
7 B B3
8 C C1
9 C C2
10 C C3
11 C C4
我想得到这个:
Clear_Time Event_Time NE_Name
2017/2/1 5:58:48 2017/2/1 5:55:54 A
2017/2/1 7:02:06 2017/2/1 6:05:30 D
2017/2/1 7:53:37 2017/2/1 7:24:21 A
我测试了很久,没解决。
我想回到NE Event_Time和Clear_Time。
我在网上搜索了很长时间。但没用。请帮助或尝试提供一些如何实现这一目标的想法。
答案 0 :(得分:0)
我认为你只关心df
df.groupby('NE_Name')[['Clear_Time', 'Event_Time']].max().reset_index()
NE_Name Clear_Time Event_Time
0 A 2017/2/1 5:58:48 2017/2/1 5:55:54
1 B 2017/2/1 7:18:36 2017/2/1 7:10:30
2 C 2017/2/1 7:53:37 2017/2/1 7:24:21
3 D 2017/2/1 7:02:06 2017/2/1 6:05:30