我有以下组成的数据,在这里,我要过滤和返回的数据点,根据最大日期/时间,只有进一步的分析。
# importing pandas library
import pandas as pd
# Hits
# importing pandas library
import pandas as pd
# Hits
player_list = [['2022-10-12 12:10',50000,1],
['2022-10-12 12:10',51000,1],
['2022-10-12 17:10',51500,1],
['2022-10-12 17:10',53000,2],
['2022-10-13 12:11',57009,2],
['2022-10-13 12:11',53001,4],
['2022-10-13 17:10',56250,4],
['2022-10-13 17:10',54000,4]]
# creating a pandas dataframe
df = pd.DataFrame(
player_list,columns = ['sanp_date',
'hits',
'state'])
df['sanp_date'] = df['sanp_date'].astype('datetime64[ns]')
# printing dataframe
print(df)
print()
# checking the type
print(df.dtypes)输出:
sanp_date hits state
0 2022-10-12 12:10:00 50000 1
1 2022-10-12 12:10:00 51000 1
2 2022-10-12 17:10:00 51500 1
3 2022-10-12 17:10:00 53000 2
4 2022-10-13 12:11:00 57009 2
5 2022-10-13 12:11:00 53001 4
6 2022-10-13 17:10:00 56250 4
7 2022-10-13 17:10:00 54000 4
sanp_date datetime64[ns]
hits int64
state int64
dtype: object我想要达到的预期结果是:
sanp_date hits state
0 2022-10-13 17:10:00 56250 4
1 2022-10-13 17:10:00 54000 4任何帮助都将不胜感激。艾伦
发布于 2022-10-13 20:06:32
你可以这么做,
df[df.sanp_date == df.sanp_date.max()]输出将是,
sanp_date hits state
6 2022-10-13 17:10:00 56250 4
7 2022-10-13 17:10:00 54000 4发布于 2022-10-13 20:04:55
df = df[df.sanp_date == df.sanp_date.max()]https://stackoverflow.com/questions/74061166
复制相似问题