U13-Forward
U13-Forward

Reputation: 71600

How to widen a dataframe - pandas

Basically i want to just flatten ( maybe not good term )

for example having dataframe:

    A      B            C
0   1  [1,2]      [1, 10]
1   2  [2, 14]    [2, 18]

I want to get the output of:

    A  B1   B2    B3   B4
0   1  1    2     1    10
1   2  2    14    2    18

I've tried:

print(pd.DataFrame(df.values.flatten().tolist(), columns=['%sG'%i for i in range(6)], index=df.index))

But nothing good.

Hope you get what i mean :)

Upvotes: 3

Views: 690

Answers (2)

U13-Forward
U13-Forward

Reputation: 71600

In more recent versions you can use explode:

>>> x = df.select_dtypes(exclude=list).join(df.select_dtypes(list).apply(pd.Series.explode, axis=1))
>>> x.columns = x.columns + x.columns.to_series().groupby(level=0).cumcount().add(1).astype(str)
>>> x
   A1  B1  B2  C1  C2
0   1   1   2   1  10
1   2   2  14   2  18
>>> 

Upvotes: 0

jezrael
jezrael

Reputation: 863166

General solution working also if lists have differents lengths:

df1 = pd.DataFrame(df['B'].values.tolist())
df2 = pd.DataFrame(df['C'].values.tolist())

df = pd.concat([df[['A']], df1, df2], axis=1)
df.columns = [df.columns[0]] + [f'B{i+1}' for i in range(len(df.columns)-1)]
print (df)
   A  B1  B2  B3  B4
0  1   1   2   1  10
1  2   2  14   2  18

If same size:

df1 = pd.DataFrame(np.array(df[['B','C']].values.tolist()).reshape(len(df),-1))
df1.columns = [f'B{i+1}' for i in range(len(df1.columns))]
df1.insert(0, 'A', df['A'])
print (df1)
   A  B1  B2  B3  B4
0  1   1   2   1  10
1  2   2  14   2  18

Upvotes: 7

Related Questions