如何通过列条件python在数据框中添加行

问题描述 投票:0回答:1

我有一个这样的数据框:

df = pd.DataFrame({'year': [2022,2022,2022,2022,2022,2022,2022,2022,2022,2022,2022,2022,2022,2022,2022,2022,2022,2022,2022],

'month': [1,2,3,1,2,3,4,5,6,7,8,9,10,11,1,2,3,4,5],
'client':[1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,3,3,3,3],
'total':[10,20,30,55,4,64,88,5,64,32,84,24,69,70,54,11,37,98,52]})
 df

    year  month  client  total
0   2022      1       1     10
1   2022      2       1     20
2   2022      3       1     30
3   2022      1       2     55
4   2022      2       2      4
5   2022      3       2     64
6   2022      4       2     88
7   2022      5       2      5
8   2022      6       2     64
9   2022      7       2     32
10  2022      8       2     84
11  2022      9       2     24
12  2022     10       2     69
13  2022     11       2     70
14  2022      1       2     54
15  2022      2       3     11
16  2022      3       3     37
17  2022      4       3     98
18  2022      5       3     52

我希望所有客户都有 12 个月,所以我需要添加此行并将值 0 分配给列总计。对于这个新行,年份列的值是已填充月份的行的副本。

欲望输出:

df = pd.DataFrame({'year': [2022,2022,2022,2022,2022,2022,2022,2022,2022,2022,2022,2022,2022,2022,2022,2022,2022,2022,2022,20 
 22,2022,2022,2022,2022,2022,2022,2022,2022,2022,2022,2022,2022,2022,2022,2022,2022],

'month': [1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11,12],
'client':[1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3],
'total': 
[10,20,30,0,0,0,0,0,0,0,0,0,55,4,64,88,5,64,32,84,24,69,70,0,54,11,37,98,52,0,0,0,0,0,0,0]})
df
    year  month  client  total  
0   2022      1       1     10
1   2022      2       1     20
2   2022      3       1     30
3   2022      4       1      0
4   2022      5       1      0
5   2022      6       1      0
6   2022      7       1      0
7   2022      8       1      0
8   2022      9       1      0
9   2022     10       1      0
10  2022     11       1      0
11  2022     12       1      0
12  2022      1       2     55
13  2022      2       2      4
14  2022      3       2     64
15  2022      4       2     88
16  2022      5       2      5
17  2022      6       2     64
18  2022      7       2     32
19  2022      8       2     84
20  2022      9       2     24
21  2022     10       2     69
22  2022     11       2     70
23  2022     12       2      0
24  2022      1       3     54
25  2022      2       3     11
26  2022      3       3     37
27  2022      4       3     98
28  2022      5       3     52
29  2022      6       3      0
30  2022      7       3      0
31  2022      8       3      0
32  2022      9       3      0
33  2022     10       3      0
34  2022     11       3      0
35  2022     12       3      0

也许通过合并选项有解决方案?我尝试过,但没有好的结果

python row addition
1个回答
0
投票

这不是最简单的方法,但它适用于

pivot_table

out = (df.pivot_table(index=['year', 'client'], columns='month',
                      values='total', fill_value=0)
         .reindex(columns=range(1, 13), fill_value=0)
         .stack().swaplevel('month', 'client')
         .rename('total').reset_index())

输出:

>>> out
    year  month  client  total
0   2022      1       1   10.0
1   2022      2       1   20.0
2   2022      3       1   30.0
3   2022      4       1    0.0
4   2022      5       1    0.0
5   2022      6       1    0.0
6   2022      7       1    0.0
7   2022      8       1    0.0
8   2022      9       1    0.0
9   2022     10       1    0.0
10  2022     11       1    0.0
11  2022     12       1    0.0
12  2022      1       2   54.5
13  2022      2       2    4.0
14  2022      3       2   64.0
15  2022      4       2   88.0
16  2022      5       2    5.0
17  2022      6       2   64.0
18  2022      7       2   32.0
19  2022      8       2   84.0
20  2022      9       2   24.0
21  2022     10       2   69.0
22  2022     11       2   70.0
23  2022     12       2    0.0
24  2022      1       3    0.0
25  2022      2       3   11.0
26  2022      3       3   37.0
27  2022      4       3   98.0
28  2022      5       3   52.0
29  2022      6       3    0.0
30  2022      7       3    0.0
31  2022      8       3    0.0
32  2022      9       3    0.0
33  2022     10       3    0.0
34  2022     11       3    0.0
35  2022     12       3    0.0
© www.soinside.com 2019 - 2024. All rights reserved.