看过来
《pandas 教程》 持续更新中,提供建议、纠错、催更等加作者微信: gr99123(备注:pandas教程)和关注公众号「盖若」ID: gairuo。跟作者学习,请进入 Python学习课程。欢迎关注作者出版的书籍:《深入浅出Pandas》 和 《Python之光》。
(编码题)某连锁超市记录了 2024 年 6 月份三家门店每天的营业额(单位:万元),数据已整理成如下 DataFrame:
日期 北京店 上海店 广州店
0 2024-06-01 9.2 10.5 8.7
1 2024-06-02 8.8 11.2 9.0
2 2024-06-03 10.1 10.8 9.5
3 2024-06-04 19.7 12.0 9.2
4 2024-06-05 10.5 11.5 9.8
请完成以下任务(全部用 pandas 内置函数,不要手写循环):
请写出完整代码并展示结果。
构造数据:
import pandas as pd
data = {
'日期': pd.date_range('2024-06-01', periods=5, freq='D'),
'北京店': [9.2, 8.8, 10.1, 19.7, 10.5],
'上海店': [10.5, 11.2, 10.8, 12.0, 11.5],
'广州店': [18.7, 9.0, 9.5, 9.2, 9.8]
}
df = pd.DataFrame(data)
df
# ...
解答代码:
# 1. 找出 **每天** 营业额最高的门店
df.set_index('日期').idxmax(axis=1)
'''
日期
2024-06-01 广州店
2024-06-02 上海店
2024-06-03 上海店
2024-06-04 北京店
2024-06-05 上海店
dtype: object
'''
# 2. 找出 **每天** 营业额最高的门店的营业额
df.set_index('日期').max(axis=1)
'''
日期
2024-06-01 18.7
2024-06-02 11.2
2024-06-03 10.8
2024-06-04 19.7
2024-06-05 11.5
dtype: float64
'''
# 3. 找出整个 6 月份 **全数据集** 中的最大营业额数值
df.set_index('日期').max().max()
# np.float64(19.7)
(完)
更新时间:2025-08-13 18:06:11 标签:pandas python 营业额