mojo1mojo2
mojo1mojo2

Reputation: 1130

Python Dataframe - How to insert row with specific index?

I am learning about dataframes and am studying various properties of animal sightings over many days. I created a dataframe that counts the number of occurrences of these animals:

>>> df
                                                       Type  Count
date                                                                
2015-07-11                                              Dog      2
2015-07-11                                              Cat      1
2015-07-12                                              Cat      2
2015-07-13                                              Pig      1

On days where the animal did not appear, I want to have the count as 0. The dataframe I want should look like:

>>> df
                                                       Type  Count
date                                                                
2015-07-11                                              Dog      2
2015-07-11                                              Cat      1
2015-07-11                                              Pig      0
2015-07-12                                              Dog      0
2015-07-12                                              Cat      2
2015-07-12                                              Pig      0
2015-07-13                                              Dog      0
2015-07-13                                              Cat      0
2015-07-13                                              Pig      1

Note: It is given that I know all the values of Type i.e. Cat, Dog, Pig.

I'm completely stumped on how to add these additional rows to the DataFrame. This is probably a very stupid question about how to append rows with a specific index.

Any help will be lovely. Thank you!

Upvotes: 0

Views: 721

Answers (1)

user2285236
user2285236

Reputation:

You can pivot & stack:

df.pivot(columns='Type').fillna(0).stack().astype(int)
Out: 
                 Count
date       Type       
2015-07-11 Cat       1
           Dog       2
           Pig       0
2015-07-12 Cat       2
           Dog       0
           Pig       0
2015-07-13 Cat       0
           Dog       0
           Pig       1

If you want one or both of them as regular columns, use reset_index():

df.pivot(columns='Type').fillna(0).stack().astype(int).reset_index()
Out: 
        date Type  Count
0 2015-07-11  Cat      1
1 2015-07-11  Dog      2
2 2015-07-11  Pig      0
3 2015-07-12  Cat      2
4 2015-07-12  Dog      0
5 2015-07-12  Pig      0
6 2015-07-13  Cat      0
7 2015-07-13  Dog      0
8 2015-07-13  Pig      1

df.pivot(columns='Type').fillna(0).stack().astype(int).reset_index(level=1)
Out: 
           Type  Count
date                  
2015-07-11  Cat      1
2015-07-11  Dog      2
2015-07-11  Pig      0
2015-07-12  Cat      2
2015-07-12  Dog      0
2015-07-12  Pig      0
2015-07-13  Cat      0
2015-07-13  Dog      0
2015-07-13  Pig      1

Upvotes: 2

Related Questions