Pandas: create rows for each unique value of a column, even with missing data

NLR

Note: I had difficulty wording the title of my question, so if you can think of something better to help other people with a similar question, please let me know and I will change it.

Current Data

Stored as a Pandas DataFrame

print(df)

week | site | vol
1    | a    | 10
2    | a    | 11
3    | a    | 2
1    | b    | 55
2    | b    | 1
1    | c    | 69
2    | c    | 66
3    | c    | 23

Notice that site b has no data for week 3

Goal

week | site | vol
1    | a    | 10
2    | a    | 11
3    | a    | 2
1    | b    | 55
2    | b    | 1
3    | b    | 0
1    | c    | 69
2    | c    | 66
3    | c    | 23

Essentially, I want to create rows for all of the unique combinations of week and site. If the original data doesn't have a vol for a week-site combo, then it gets a 0.

BENY

Using stack with unstack

df.set_index(['week','site']).unstack('week',fill_value=0).stack().reset_index()
Out[424]: 
  site  week  vol
0    a     1   10
1    a     2   11
2    a     3    2
3    b     1   55
4    b     2    1
5    b     3    0
6    c     1   69
7    c     2   66
8    c     3   23

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Is there a pandas function to transpose a data frame to create a separate column for each unique value of an existing column?

How to create new date rows for each unique value in column?

Create n rows up to highest value in column for each unique client

Create new dataframes for each unique value in a column in pandas

Create Multiple rows for each value in given column in pandas df

Python Pandas - filter pandas dataframe to get rows with minimum values in one column for each unique value in another column

Pandas Dataframe duplicate rows with mean-based on the unique value in one column and so that each unique value have same number of rows

Python Pandas pivoting: how to group in the first column and create a new column for each unique value from the second column

Pandas Calculation Grouped By Unique Rows of Column Value

find a fix value from a column around a range with each unique values of another column in pandas data frame

Python pandas capture exisiting data value for each column data, concatenate new values and ensure values are unique per column data

How to find minimum for each unique value for every column grouped by ID in pandas data frame

Pandas: check a sequence in one column for each unique value in another column

MySQL get n percent of rows matching each unique column value

How to convert column values to rows for each unique value in a dataframe in R?

Select the X newest rows for each unique value of a column

Extract all rows containing second unique value for each column by group

Merge all the data in the second column for each unique value in the first column

Replicating rows in a pandas data frame by a column value

Oracle - produce unique rows for each unique column value and convert rows to columns

Remove Columns And Create Unique Row For Each Removed Column Pandas Dataframe

Filtering rows that have unique value in a column using pandas

Pandas: get unique rows with unique value in column grouped by another column value

Create columns based on count of each unique value of one column in Pig

Create additional rows in data frame in R for unique elements in column

Create a Boolean column for unique rows in a grouped data frame

Python Pandas - Map each key & value to a unique column

Count occurrences of each unique value within a column pandas

Pandas: count unique value in each column, by looping through them?