Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 求组合最大值_Python_Pandas_Dataframe - Fatal编程技术网

Python 求组合最大值

Python 求组合最大值,python,pandas,dataframe,Python,Pandas,Dataframe,我有以下数据帧: {'date': '2019-10-21', 'hour': 3, 'id': '1'}, {'date': '2019-10-21', 'hour': 4, 'id': '1'}, {'date': '2019-10-20', 'hour': 0, 'id': '1'}, {'date': '2019-10-20', 'hour': 1, 'id': '1'}, {'date': '2019-10-21', 'hour': 0, 'id': '1'}, {'date': '2

我有以下数据帧:

{'date': '2019-10-21', 'hour': 3, 'id': '1'},
{'date': '2019-10-21', 'hour': 4, 'id': '1'},
{'date': '2019-10-20', 'hour': 0, 'id': '1'},
{'date': '2019-10-20', 'hour': 1, 'id': '1'},
{'date': '2019-10-21', 'hour': 0, 'id': '1'},
{'date': '2019-10-20', 'hour': 0, 'id': '1'},
{'date': '2019-10-19', 'hour': 5, 'id': '1'},
{'date': '2019-10-20', 'hour': 0, 'id': '2'},
{'date': '2019-10-20', 'hour': 0, 'id': '3'}
我需要为每个id查找最新的日期和时间,例如,对于id=1,我需要2019-10-21和4,同时我要获得正确的日期,但时间=5,供所有3列使用,并按列删除重复项
id

L = [{'date': '2019-10-21', 'hour': 3, 'id': '1'},
{'date': '2019-10-21', 'hour': 4, 'id': '1'},
{'date': '2019-10-20', 'hour': 0, 'id': '1'},
{'date': '2019-10-20', 'hour': 1, 'id': '1'},
{'date': '2019-10-21', 'hour': 0, 'id': '1'},
{'date': '2019-10-20', 'hour': 0, 'id': '1'},
{'date': '2019-10-19', 'hour': 5, 'id': '1'},
{'date': '2019-10-20', 'hour': 0, 'id': '2'},
{'date': '2019-10-20', 'hour': 0, 'id': '3'}]

df = pd.DataFrame(L)
df['date'] = pd.to_datetime(df['date'])

df = df.sort_values(['id','date','hour'], ascending=[True, False, False]).drop_duplicates('id')
print (df)
        date  hour id
1 2019-10-21     4  1
7 2019-10-20     0  2
8 2019-10-20     0  3