python - 日期格式以及如何打印出现的最大数字和日期
问题描述
在以下数据中,我想获得发生的最多次数以及Swings
发生在什么上Date
。我已经可以获得发生的最大摇摆次数,但不确定如何获得相应的日期:
data = [
{"Date": "01/06/2020", "Swings": 350},
{"Date": "02/06/2020", "Swings": 370},
]
df = pd.DataFrame(data)
max_swings = max(df['Swings'])
print ("The max number of swings in one day was: " + str(max_swings))
(注:我使用 DD/MM/YYYY 日期格式)
解决方案
通常我们将日期转换为datetime
.
df['Date'] = pd.to_datetime(df['Date']) # convert to datetime
df.iloc[df['Swings'].idxmax()] # this gives you the row with the highest swings
df['Swings'].idxmax()
为您提供 Swings 最大值所在的第一次出现的索引。然后您可以使用.iloc[]
推荐阅读
- angular - ag-grid:使用服务器端行模型的行分组问题(ag-grid 版本 22.1.0)
- python - Why python if command doesnt work correctly?
- reactjs - Refresh part of page in a service worker push event in React
- windows - 尽管指定了另一个驱动器进行安装,为什么 Visual Studio 仍坚持在系统驱动器上安装部分组件?
- asp.net - 如何在站点 web.config 级别禁用 IIS 10 中的 brotli httpCompression?
- javascript - D3js图例颜色与地图颜色javascript不匹配
- powerbi - 创建一个过滤器,显示过去 12 小时的数据功率 bi
- katalon-studio - Katalon 测试集合构建在 Jenkins 上失败
- vitess - 启动 vreplication 引擎时出错:通过 connection 连接到 mysql db 时出错
Kubernetes 上的 Vitess - neo4j - 如何在 neo4j 中的两个属性上创建唯一约束?