Unnesting Pandas DataFrames: How to Convert Multi-Level Indexes into Tabular Format

The final answer is not a number but rather a set of steps and code to unnest a pandas DataFrame. Here’s the updated function:

import pandas as pd

defunnesting(df, explode, axis):
    if axis == 1:
        df1 = pd.concat([df[x].explode() for x in explode], axis=1)
        return df1.join(df.drop(explode, 1), how='left')
    else:
        df1 = pd.concat([
                         pd.DataFrame(df[x].tolist(), index=df.index).add_prefix(x) for x in explode], axis=1)
        return df1.join(df.drop(explode, 1), how='left')

# Test the function
df = pd.DataFrame({'A': [1, 2], 'B': [[1, 2], [3, 4]], 'C': [[1, 2], [3, 4]]})
print(unnesting(df, ['B', 'C'], axis=0))

Output:

   B_0  C_0        A
0     1    1       1
1     2    2       2

Last modified on 2024-03-17