python - 插入行并添加缺失的数据

标签 python pandas insert nan reindex

我想知道是否有人可以就如何进行以下操作提供一些指导。作为 Pandas 的新手,我觉得目前我的整体知识和技能水平还不足以处理我在下面概述的请求。

我有一个 pandas 数据框,其中包含 2000 多个零件号的列表。对于每个零件,都有零件的销售年份、月份数、销售数量和销售值(value)。每年,可能偶尔会有缺失的月份。在下面显示的 2007 年数据示例中,缺少第 11 个月,因为该月没有销售。与 2008 年类似,缺少第 11 个月和第 12 个月。我想要做的是插入每年缺失的月份,并插入一行,其中包含适当的年份、月份以及每个part_id组中数量和销售额的零值。
总共的数据约为。 60200,行数约为。 2000 个零件 ID。 我不介意花时间开发解决方案,但可以通过一些指导来帮助我的教育。

index                     Part_ID  Year     Month    Qty           Sales
60182                       ZZSSL  2007      5       11.0          724.85   
60183                       ZZSSL  2007      6        7.0          537.94   
60184                       ZZSSL  2007      7       17.0         1165.02   
60185                       ZZSSL  2007      8        3.0          159.56   
60186                       ZZSSL  2007      9       67.0         4331.28   
60187                       ZZSSL  2007     10       72.0         4582.98   
60188                       ZZSSL  2007     12       42.0         2651.42   
60189                       ZZSSL  2008      1       22.0         1422.32   
60190                       ZZSSL  2008      2       16.0         1178.98   
60191                       ZZSSL  2008      3       20.0         1276.60   
60192                       ZZSSL  2008      4       28.0         2120.84   
60193                       ZZSSL  2008      5        2.0           83.03   
60194                       ZZSSL  2008      6       16.0         1250.24   
60195                       ZZSSL  2008      9       17.0         1323.34   
60196                       ZZSSL  2008     10        2.0          197.98   
60197                       ZZSSL  2009      1       21.0         1719.30   
60198                       ZZSSL  2009      2        1.0           78.15   
60199                       ZZSSL  2009      3        3.0          281.34   
60200                       ZZSSL  2009      4       25.0         2214.25   
60201                       ZZSSL  2009      5       10.0          833.60   
60202                       ZZSSL  2009      6        1.0           83.36   
60203                       ZZSSL  2009      7        1.0           83.36

最佳答案

我认为你首先需要 set_index ,然后 unstackreindexMultiIndexfrom_product 创建的列与 stack :

mux = pd.MultiIndex.from_product([['Qty','Sales'],np.arange(1,13)])

print (df.set_index(['Part_ID','Year', 'Month'])
         .unstack(fill_value=0)
         .reindex(columns=mux, fill_value=0)
         .stack()
         .rename_axis(['Part_ID','Year','Month'])
         .reset_index())
   Part_ID  Year  Month   Qty    Sales
0    ZZSSL  2007      1   0.0     0.00
1    ZZSSL  2007      2   0.0     0.00
2    ZZSSL  2007      3   0.0     0.00
3    ZZSSL  2007      4   0.0     0.00
4    ZZSSL  2007      5  11.0   724.85
5    ZZSSL  2007      6   7.0   537.94
6    ZZSSL  2007      7  17.0  1165.02
7    ZZSSL  2007      8   3.0   159.56
8    ZZSSL  2007      9  67.0  4331.28
9    ZZSSL  2007     10  72.0  4582.98
10   ZZSSL  2007     11   0.0     0.00
11   ZZSSL  2007     12  42.0  2651.42
12   ZZSSL  2008      1  22.0  1422.32
13   ZZSSL  2008      2  16.0  1178.98
14   ZZSSL  2008      3  20.0  1276.60
15   ZZSSL  2008      4  28.0  2120.84
16   ZZSSL  2008      5   2.0    83.03
17   ZZSSL  2008      6  16.0  1250.24
18   ZZSSL  2008      7   0.0     0.00
19   ZZSSL  2008      8   0.0     0.00
20   ZZSSL  2008      9  17.0  1323.34
21   ZZSSL  2008     10   2.0   197.98
22   ZZSSL  2008     11   0.0     0.00
23   ZZSSL  2008     12   0.0     0.00
24   ZZSSL  2009      1  21.0  1719.30
25   ZZSSL  2009      2   1.0    78.15
26   ZZSSL  2009      3   3.0   281.34
27   ZZSSL  2009      4  25.0  2214.25
28   ZZSSL  2009      5  10.0   833.60
29   ZZSSL  2009      6   1.0    83.36
30   ZZSSL  2009      7   1.0    83.36
31   ZZSSL  2009      8   0.0     0.00
32   ZZSSL  2009      9   0.0     0.00
33   ZZSSL  2009     10   0.0     0.00
34   ZZSSL  2009     11   0.0     0.00
35   ZZSSL  2009     12   0.0     0.00

如果只需要缺失每个开始和结束月份之间的值:

df['Month'] = pd.to_datetime(df.Month.astype(str) + '-01-' 
                                                  + df.Year.astype(str))
df = df.set_index('Month')
       .groupby(['Part_ID','Year'])
       .resample('MS')
       .asfreq()
       .fillna(0)
       .drop(['Part_ID','Year'], axis=1)
       .reset_index()
df['Month'] = df['Month'].dt.month 
print (df)
   Part_ID  Year  Month   Qty    Sales
0    ZZSSL  2007      5  11.0   724.85
1    ZZSSL  2007      6   7.0   537.94
2    ZZSSL  2007      7  17.0  1165.02
3    ZZSSL  2007      8   3.0   159.56
4    ZZSSL  2007      9  67.0  4331.28
5    ZZSSL  2007     10  72.0  4582.98
6    ZZSSL  2007     11   0.0     0.00
7    ZZSSL  2007     12  42.0  2651.42
8    ZZSSL  2008      1  22.0  1422.32
9    ZZSSL  2008      2  16.0  1178.98
10   ZZSSL  2008      3  20.0  1276.60
11   ZZSSL  2008      4  28.0  2120.84
12   ZZSSL  2008      5   2.0    83.03
13   ZZSSL  2008      6  16.0  1250.24
14   ZZSSL  2008      7   0.0     0.00
15   ZZSSL  2008      8   0.0     0.00
16   ZZSSL  2008      9  17.0  1323.34
17   ZZSSL  2008     10   2.0   197.98
18   ZZSSL  2009      1  21.0  1719.30
19   ZZSSL  2009      2   1.0    78.15
20   ZZSSL  2009      3   3.0   281.34
21   ZZSSL  2009      4  25.0  2214.25
22   ZZSSL  2009      5  10.0   833.60
23   ZZSSL  2009      6   1.0    83.36
24   ZZSSL  2009      7   1.0    83.36

关于python - 插入行并添加缺失的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39458148/

相关文章:

python - Julia 性能与 Python+Numba LLVM/JIT 编译代码的比较

python - 如何将列表中的空项更改为 N/a 值?

python - Pandas 中将元组分配给段的最有效方法

insert - SparQL 插入的 org.openrdf.query.MalformedQueryException

sql - 对多行使用 SELECT INTO

python - 创建形状文件

python - 如何从 python 中的字符串中删除 ANSI 转义序列

python - 如何使用 python 根据日期接近程度的特定条件删除重复项?

pandas - 根据另一个数据框 python pandas 替换列值? (初学者)

mysql 插入错误...不知道出了什么问题