Vinnton
Vinnton

Reputation: 57

Fill missing rows with zeros from a data frame

Now I have a DataFrame as below:

video_id  0   1   2   3    4   5   6   7   8   9  ...  53   54  55  56  
user_id                                           ...                        
0          0   0   0   0    0   0   0   0   0   0 ...   0    0   0   0     
1          2   0   4  13   16   2   0  10   6  45 ...   3  352   6   0    
2          0   0   0   0    0   0   0  11   0   0 ...   0    0   0   0     
3          4  13   0   8    0   0   5   9  12  11 ...  14   17   0   6     
4          0   0   4  13   25   4   0  33   0  39 ...   5    7   4   3     
6          2   0   0   0   12   0   0   0   2   0 ...  19    4   0   0     
7         33  59  52  59  113  53  29  32  59  82 ...  60  119  57  39     
9          0   0   0   0    5   0   0   1   0   4 ...  16    0   0   0     
10         0   0   0   0   40   0   0   0   0   0 ...  26    0   0   0     
11         2   2  32   3   12   3   3  11  19  10 ...  16    3   3   9    
12         0   0   0   0    0   0   0   7   0   0 ...   7    0   0   0     

We can see that part of the DataFrame is missing, like user_id_5 and user_id_8. What I want to do is to fill these rows with 0, like:

video_id  0   1   2   3    4   5   6   7   8   9  ...  53   54  55  56  
user_id                                           ...                        
0          0   0   0   0    0   0   0   0   0   0 ...   0    0   0   0     
1          2   0   4  13   16   2   0  10   6  45 ...   3  352   6   0    
2          0   0   0   0    0   0   0  11   0   0 ...   0    0   0   0     
3          4  13   0   8    0   0   5   9  12  11 ...  14   17   0   6     
4          0   0   4  13   25   4   0  33   0  39 ...   5    7   4   3
5          0   0   0   0    0   0   0   0   0   0 ...   0    0   0   0
6          2   0   0   0   12   0   0   0   2   0 ...  19    4   0   0     
7         33  59  52  59  113  53  29  32  59  82 ...  60  119  57  39 
8          0   0   0   0    0   0   0   0   0   0 ...   0    0   0   0    
9          0   0   0   0    5   0   0   1   0   4 ...  16    0   0   0     
10         0   0   0   0   40   0   0   0   0   0 ...  26    0   0   0     
11         2   2  32   3   12   3   3  11  19  10 ...  16    3   3   9    
12         0   0   0   0    0   0   0   7   0   0 ...   7    0   0   0 

Is there any solution to this issue?

Upvotes: 0

Views: 127

Answers (1)

cs95
cs95

Reputation: 403218

You could use arange + reindex -

df = df.reindex(np.arange(df.index.min(), df.index.max() + 1), fill_value=0)

Assuming your index is meant to be monotonically increasing index.


df

     0   1   2   3    4   5   6   7   8   9
0    0   0   0   0    0   0   0   0   0   0
1    2   0   4  13   16   2   0  10   6  45
2    0   0   0   0    0   0   0  11   0   0
3    4  13   0   8    0   0   5   9  12  11
4    0   0   4  13   25   4   0  33   0  39
6    2   0   0   0   12   0   0   0   2   0
7   33  59  52  59  113  53  29  32  59  82
9    0   0   0   0    5   0   0   1   0   4
10   0   0   0   0   40   0   0   0   0   0
11   2   2  32   3   12   3   3  11  19  10
12   0   0   0   0    0   0   0   7   0   0

df.reindex(np.arange(df.index.min(), df.index.max() + 1), fill_value=0)

     0   1   2   3    4   5   6   7   8   9
0    0   0   0   0    0   0   0   0   0   0
1    2   0   4  13   16   2   0  10   6  45
2    0   0   0   0    0   0   0  11   0   0
3    4  13   0   8    0   0   5   9  12  11
4    0   0   4  13   25   4   0  33   0  39
5    0   0   0   0    0   0   0   0   0   0    # <----- 
6    2   0   0   0   12   0   0   0   2   0
7   33  59  52  59  113  53  29  32  59  82
8    0   0   0   0    0   0   0   0   0   0    # <----- 
9    0   0   0   0    5   0   0   1   0   4
10   0   0   0   0   40   0   0   0   0   0
11   2   2  32   3   12   3   3  11  19  10
12   0   0   0   0    0   0   0   7   0   0

Upvotes: 2

Related Questions