2

I have a dataframe like the following:

df
    2    4   5
0   1    3   2 
3  -1    4   5
5   3   -6   7

I want to fill the missing values in the index and in the columns and fill the values with zeros, so that:

df
    0   1   2   3   4    5
0   0   0   1   0   3    2 
1   0   0   0   0   0    0
2   0   0   0   0   0    0
3   0   0  -1   0   4    5
4   0   0   0   0   0    0
5   0   0   3   0  -6    7

1 Answer 1

4

Use reindex by np.arange:

df = (df.reindex(index = np.arange(df.index.max() + 1), 
                columns = np.arange(df.columns.max() + 1), 
                fill_value=0))
print (df)
   0  1  2  3  4  5
0  0  0  1  0  3  2
1  0  0  0  0  0  0
2  0  0  0  0  0  0
3  0  0 -1  0  4  5
4  0  0  0  0  0  0
5  0  0  3  0 -6  7
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.