Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 插入行并添加缺少的数据_Python_Pandas_Insert_Nan_Reindex - Fatal编程技术网

Python 插入行并添加缺少的数据

Python 插入行并添加缺少的数据,python,pandas,insert,nan,reindex,Python,Pandas,Insert,Nan,Reindex,我想知道是否有人能就如何进行以下工作给出一些建议。作为熊猫队的新手,我觉得目前我的整体知识和技能水平还不足以满足我下面概述的要求 我有一个熊猫数据框,它有一个大约2000多个零件号的列表。对于每个零件,都有零件的销售年份、月号、销售数量和销售价值。每一年,都有可能出现偶尔的失踪月份。在下面显示的2007年示例数据中,第11个月缺失,因为该月没有销售。与2008年类似,第11个月和第12个月也不见了。我想做的是插入每年缺少的月份,并插入一行,其中包含每个零件id组中数量和销售额的适当年份、月份和零

我想知道是否有人能就如何进行以下工作给出一些建议。作为熊猫队的新手,我觉得目前我的整体知识和技能水平还不足以满足我下面概述的要求

我有一个熊猫数据框,它有一个大约2000多个零件号的列表。对于每个零件,都有零件的销售年份、月号、销售数量和销售价值。每一年,都有可能出现偶尔的失踪月份。在下面显示的2007年示例数据中,第11个月缺失,因为该月没有销售。与2008年类似,第11个月和第12个月也不见了。我想做的是插入每年缺少的月份,并插入一行,其中包含每个零件id组中数量和销售额的适当年份、月份和零值。
数据总量约为60200行,零件id约为2000。 我不介意花时间来制定解决方案,但可以通过一些建议来帮助我的教育

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
我认为您首先需要,然后需要使用创建自以下内容的
MultiIndex
和列:

如果只需要在每个
年份的开始和结束
月份
之间缺少值

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
试试这个:

In [220]: r = (df.reset_index()
   .....:        .set_index(pd.to_datetime(df.Year.map(str) + '-' + df.Month.map(str).str.zfill(2) + '-01'))
   .....:        .resample('MS')
   .....: )

In [221]: new = r.pad().drop(['Qty','Sales'],1).join(r.mean().replace(np.nan, 0)[['Qty','Sales']])

In [222]: new.Month = new.index.month

In [223]: new.reset_index(drop=True)
Out[223]:
    index Part_ID  Year  Month   Qty    Sales
0   60182   ZZSSL  2007      5  11.0   724.85
1   60183   ZZSSL  2007      6   7.0   537.94
2   60184   ZZSSL  2007      7  17.0  1165.02
3   60185   ZZSSL  2007      8   3.0   159.56
4   60186   ZZSSL  2007      9  67.0  4331.28
5   60187   ZZSSL  2007     10  72.0  4582.98
6   60187   ZZSSL  2007     11   0.0     0.00
7   60188   ZZSSL  2007     12  42.0  2651.42
8   60189   ZZSSL  2008      1  22.0  1422.32
9   60190   ZZSSL  2008      2  16.0  1178.98
10  60191   ZZSSL  2008      3  20.0  1276.60
11  60192   ZZSSL  2008      4  28.0  2120.84
12  60193   ZZSSL  2008      5   2.0    83.03
13  60194   ZZSSL  2008      6  16.0  1250.24
14  60194   ZZSSL  2008      7   0.0     0.00
15  60194   ZZSSL  2008      8   0.0     0.00
16  60195   ZZSSL  2008      9  17.0  1323.34
17  60196   ZZSSL  2008     10   2.0   197.98
18  60196   ZZSSL  2008     11   0.0     0.00
19  60196   ZZSSL  2008     12   0.0     0.00
20  60197   ZZSSL  2009      1  21.0  1719.30
21  60198   ZZSSL  2009      2   1.0    78.15
22  60199   ZZSSL  2009      3   3.0   281.34
23  60200   ZZSSL  2009      4  25.0  2214.25
24  60201   ZZSSL  2009      5  10.0   833.60
25  60202   ZZSSL  2009      6   1.0    83.36
26  60203   ZZSSL  2009      7   1.0    83.36

嗨,耶斯雷尔,谢谢你的回答。我希望每个月每年只出现一次。我将研究堆栈和取消堆栈。仍然有遗漏的月份。例如11感谢所有提供答案的人,我将研究响应作为学习点,并确保我了解发生了什么。这是一个很棒的网站。
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
In [220]: r = (df.reset_index()
   .....:        .set_index(pd.to_datetime(df.Year.map(str) + '-' + df.Month.map(str).str.zfill(2) + '-01'))
   .....:        .resample('MS')
   .....: )

In [221]: new = r.pad().drop(['Qty','Sales'],1).join(r.mean().replace(np.nan, 0)[['Qty','Sales']])

In [222]: new.Month = new.index.month

In [223]: new.reset_index(drop=True)
Out[223]:
    index Part_ID  Year  Month   Qty    Sales
0   60182   ZZSSL  2007      5  11.0   724.85
1   60183   ZZSSL  2007      6   7.0   537.94
2   60184   ZZSSL  2007      7  17.0  1165.02
3   60185   ZZSSL  2007      8   3.0   159.56
4   60186   ZZSSL  2007      9  67.0  4331.28
5   60187   ZZSSL  2007     10  72.0  4582.98
6   60187   ZZSSL  2007     11   0.0     0.00
7   60188   ZZSSL  2007     12  42.0  2651.42
8   60189   ZZSSL  2008      1  22.0  1422.32
9   60190   ZZSSL  2008      2  16.0  1178.98
10  60191   ZZSSL  2008      3  20.0  1276.60
11  60192   ZZSSL  2008      4  28.0  2120.84
12  60193   ZZSSL  2008      5   2.0    83.03
13  60194   ZZSSL  2008      6  16.0  1250.24
14  60194   ZZSSL  2008      7   0.0     0.00
15  60194   ZZSSL  2008      8   0.0     0.00
16  60195   ZZSSL  2008      9  17.0  1323.34
17  60196   ZZSSL  2008     10   2.0   197.98
18  60196   ZZSSL  2008     11   0.0     0.00
19  60196   ZZSSL  2008     12   0.0     0.00
20  60197   ZZSSL  2009      1  21.0  1719.30
21  60198   ZZSSL  2009      2   1.0    78.15
22  60199   ZZSSL  2009      3   3.0   281.34
23  60200   ZZSSL  2009      4  25.0  2214.25
24  60201   ZZSSL  2009      5  10.0   833.60
25  60202   ZZSSL  2009      6   1.0    83.36
26  60203   ZZSSL  2009      7   1.0    83.36