My dataframe looks like this:
| column1 | sum_count |
|---|---|
| 1 | 10 |
| 3 | 2 |
| 5 | 1 |
However, there are 5 values in column1 (1-5). I would like to fill in 0s for values that do not have a sum_count like this:
| column1 | sum_count |
|---|---|
| 1 | 10 |
| 2 | 0 |
| 3 | 2 |
| 4 | 0 |
| 5 | 1 |
How would I do this?
CodePudding user response:
can use the .reindex facility but "column1" needs to go to index and come back:
In [154]: (df.set_index("column1")
...: .reindex(range(1, 5 1), fill_value=0)
...: .reset_index())
Out[154]:
column1 sum_count
0 1 10
1 2 0
2 3 2
3 4 0
4 5 1
for dynamism in 1 and 5:
- 1 is
df.column1.iat[0], 5 isdf.column1.iat[-1]OR df.column1.iloc[[0, -1]]to get both with one expr OR- 1 is
df.column1.min(), 5 isdf.column1.max()
