我如何填写此日期框中缺失的数据。
没有销售的日子里缺少值。如何填写在特定商店和日期售出 0 件商品的天数的缺失值?
Input
Dates Store Item Sales
2017-01-01 Chicago Apple 10
2017-01-02 NewYork Pear 10
2017-01-03 Chicago Apple 10
Output
Dates Store Item Sales
2017-01-01 Chicago Apple 10
2017-01-01 Chicago Pear 0
2017-01-02 Chicago Apple 0
2017-01-02 Chicago Pear 0
2017-01-03 Chicago Apple 10
2017-01-03 Chicago Pear 0
2017-01-01 NewYork Apple 0
2017-01-01 NewYork Pear 0
2017-01-02 NewYork Apple 0
2017-01-02 NewYork Pear 10
2017-01-03 NewYork Apple 0
2017-01-03 NewYork Pear 0
Use:
- first set_index http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.set_index.html for
Multiindex
- 创建新的
Multiindex
from_product http://pandas.pydata.org/pandas-docs/stable/generated/pandas.MultiIndex.from_product.html
-
reindex http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.reindex.html并添加
0
对于缺失值
- 最后排序级别
Store
by sort_index http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.sort_index.html and reset_index http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.reset_index.html
df = df.set_index(['Dates','Store','Item'])
mux = pd.MultiIndex.from_product(df.index.levels, names=df.index.names)
df = df.reindex(mux, fill_value=0).sort_index(level='Store').reset_index()
print (df)
Dates Store Item Sales
0 2017-01-01 Chicago Apple 10
1 2017-01-01 Chicago Pear 0
2 2017-01-02 Chicago Apple 0
3 2017-01-02 Chicago Pear 0
4 2017-01-03 Chicago Apple 10
5 2017-01-03 Chicago Pear 0
6 2017-01-01 NewYork Apple 0
7 2017-01-01 NewYork Pear 0
8 2017-01-02 NewYork Apple 0
9 2017-01-02 NewYork Pear 10
10 2017-01-03 NewYork Apple 0
11 2017-01-03 NewYork Pear 0
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)