Linked Questions

Popular Questions

Merging pandas DataFrames with NaN for missing rows

Asked by At

I want to use my reference calendar as a scaffold to fill up missing data in my main data. For that purpose I want to join these two dataframes.

import pandas as pd
import numpy as np

d1 = { 'Year': [2019,2019,2019,2019,2019,2019],
        'Week': [1,2,3,5,5,6],
        'Part': ['A','A','A','A','B','B'],
        'Static': [20,20,20,20,40,40],
        'Value': [np.nan,10,np.nan,50,30,np.nan] }

d2 = { 'Year':[2019,2019,2019,2019,2019,2019,2019,2019,2019,2019],
        'Week':[1,2,3,4,5,6,7,8,9,10] }

df1 = pd.DataFrame(d1)
df2 = pd.DataFrame(d2)

The expected output is as follows

    Year  Week Part  Static  Value
0   2019     1    A      20    NaN
1   2019     2    A      20   10.0
2   2019     3    A      20    NaN
3   2019     4    A      20    NaN
4   2019     5    A      20   50.0
5   2019     6    A      20    NaN
6   2019     7    A      20    NaN
7   2019     8    A      20    NaN
8   2019     9    A      20    NaN
9   2019    10    A      20    NaN
10  2019     1    B      40    NaN
11  2019     2    B      40    NaN
12  2019     3    B      40    NaN
13  2019     4    B      40    NaN
14  2019     5    B      40   30.0
15  2019     6    B      40    NaN
16  2019     7    B      40    NaN
17  2019     8    B      40    NaN
18  2019     9    B      40    NaN
19  2019    10    B      40    NaN

Related Questions