Search code examples
pythonpandas

Stack a Pandas dataframe replacing NaNs


I was having trouble finding this answered already. I have sample data below:

df = pd.DataFrame({'Name': {0: 'James', 1: 'James', 2: 'James', 3: 'John', 4: 'Johh', 5: 'John'},
                  'Final': {0: 'foo', 1: np.nan, 2: np.nan, 3: 'too', 4: np.nan, 5: np.nan},
                  'Alpha': {0: 'X', 1: 'Y', 2: 'Z', 3: 'X', 4: 'Y', 5: 'Z'},
                    'Two': {0: '1234', 1: '5678', 2:np.nan , 3: 'abdc', 4: 'efgh', 5: np.nan} 
                 })

enter image description here

I want to transform this so that all of the data is on top of each other and replacing the null values like this:

enter image description here


Solution

  • Try this to get a series, add .to_frame() to get a dataframe:

    df.set_index('Name').stack().reset_index(level=1, drop=True)
    

    Output:

    Name
    James     foo
    James       X
    James    1234
    James       Y
    James    5678
    James       Z
    John      too
    John        X
    John     abdc
    Johh        Y
    Johh     efgh
    John        Z
    dtype: object