看过来
《pandas 教程》 持续更新中,提供建议、纠错、催更等加作者微信: gr99123(备注:pandas教程)和关注公众号「盖若」ID: gairuo。跟作者学习,请进入 Python学习课程。欢迎关注作者出版的书籍:《深入浅出Pandas》 和 《Python之光》。
(编码题)销售部导出 2024-08-01 至 2024-08-10 的每日 GMV(万元),中间存在缺失:
import pandas as pd
import numpy as np
sales = pd.Series(
[100, np.nan, 120, np.nan, 135, 130, np.nan, 145, 150, np.nan],
index=pd.date_range('2024-08-01', periods=10, freq='D'),
name='GMV'
)
sales
'''
2024-08-01 100.0
2024-08-02 NaN
2024-08-03 120.0
2024-08-04 NaN
2024-08-05 135.0
2024-08-06 130.0
2024-08-07 NaN
2024-08-08 145.0
2024-08-09 150.0
2024-08-10 NaN
Freq: D, Name: GMV, dtype: float64
'''
完成以下补全策略,并打印结果:
(
sales.ffill()
.bfill()
)
'''
2024-08-01 100.0
2024-08-02 100.0
2024-08-03 120.0
2024-08-04 120.0
2024-08-05 135.0
2024-08-06 130.0
2024-08-07 130.0
2024-08-08 145.0
2024-08-09 150.0
2024-08-10 150.0
Freq: D, Name: GMV, dtype: float64
'''
(完)
更新时间:2025-08-21 17:41:31 标签:pandas python 销售额 缺失值