0

Good Afternoon!

I have a pandas dataframe with an index and a count.

dictionary = {1:5,2:10,4:3,5:2}
df = pd.DataFrame.from_dict(dictionary , orient = 'index' , columns = ['count'])

What I want to do is check from df.index.min() to df.index.max() that the index increment is 1. If a value is missing like in my case the 3 is missing then I want to add 3 to the index with a 0 in the count.

The output will look like the below df2 but done in a programmatic fashion so I can use it on a much bigger dataframe.

RESULTS EXAMPLE DF:

dictionary2 = {1:5,2:10,3:0,4:3,5:2}
df2 = pd.DataFrame.from_dict(dictionary2 , orient = 'index' , columns = ['count'])

Thank you much!!!

1 Answer 1

1

Ensure the index is sorted:

df = df.sort_index()

Create an array that starts from the minimum index to the maximum index

   complete_array = np.arange(df.index.min(), df.index.max() + 1)

Reindex, fill the null value with 0, and optionally change the dtype to Pandas Int:

df.reindex(complete_array, fill_value=0).astype("Int16")

    count
1   5
2   10
3   0
4   3
5   2
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.