本文介绍了填写缺失的数据 pandas 的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在此日期范围内填写缺失的数据.
How can I fill in the missing data in this dateframe.
未进行销售的天数缺少值.我该如何填写某商品在特定商店和日期售出的0天中的缺失值?
Missing values for days when no sales are made. How can I fill in the missing values for days where 0 of an item were sold at a particular store and date?
输入
Dates Store Item Sales
2017-01-01 Chicago Apple 10
2017-01-02 NewYork Pear 10
2017-01-03 Chicago Apple 10
输出
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
推荐答案
使用:
- 首先
set_index
用于Multiindex
- 创建新的
Multiindex
from_product
-
reindex
并添加0
缺少值 - 最后排序级别
Store
,由sort_index
和reset_index
- first
set_index
forMultiindex
- create new
Multiindex
from_product
reindex
and add0
for missing values- last sort level
Store
bysort_index
andreset_index
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
这篇关于填写缺失的数据 pandas 的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!