Pandas: Expand/Explode Dataframe Horizontally

Viewed 749

This may be a duplicate, but I can't find the required answer. So, here's the question:

Suppose, I have got a dataframe like this:

d1 = {'col1':  [[1],[2,3]],
      'col2' : [[3],[21,1]]}

df1 = pd.DataFrame(d1)
col1 col2
0 [1] [3]
1 [2, 3] [21, 1]

Now, we can expand this dataframe vertically very easily via df1.apply(pd.Series.explode). But, what's the most elegant way to expand in a horizontal direction and change the column names?

Something like this:

d2 = {
    'col1_1':[1,2],
    'col1_2': [np.NAN,3],
    'col2_1' : [3,21],
    'col2_2' : [np.NAN,1]
}
df2 = pd.DataFrame(d2)

Output:

col1_1 col1_2 col2_1 col2_2
0 1 NaN 3 NaN
1 2 3.0 21 1.0
2 Answers
x = pd.concat(
    [df1[c].apply(pd.Series).add_prefix(c + "_") for c in df1], axis=1
)
print(x)

Prints:

   col1_0  col1_1  col2_0  col2_1
0     1.0     NaN     3.0     NaN
1     2.0     3.0    21.0     1.0

If you want 1-based indexed columns:

x = pd.concat(
    [df1[c].apply(pd.Series).add_prefix(c + "_") for c in df1], axis=1
).rename(
    columns=lambda x: "{}_{}".format(x.split("_")[0], int(x.split("_")[1]) + 1)
)
print(x)

Prints:

   col1_1  col1_2  col2_1  col2_2
0     1.0     NaN     3.0     NaN
1     2.0     3.0    21.0     1.0

Try this.

d1 = {'col1':  [[1],[2,3]],
      'col2' : [[3],[21,1]]}

df1 = pd.DataFrame(d1)

col_names = []
for col in list(df1):
    for col_number in range(max(df1[col].apply(len))):
        col_names.append(col + "_" + str(col_number + 1))

df2 = pd.concat([pd.DataFrame(df1.col1.tolist(), index= df1.index), pd.DataFrame(df1.col2.tolist(), index = df1.index)], axis = 1)
df2.columns = col_names

Related