jane1912
jane1912

Reputation: 85

convert list values to rows in pandas

I have dataframe where one of the column has numpy.ndarray values with same length,

df[list][0]
Out[92]: 
array([0.        , 0.        , 0.        , ..., 0.29273096, 0.30691767,
       0.27531403])

I would like to convert these list values to be a dataframe and filled as single column value from df.iloc[,1:len(list)]

Example

   list     1         2         3     ...
0  [..]  0         0         0
1  [..]  0.570642  0.181552  0.794599
2  [..]  0.568440  0.501638  0.186635
3  [..]  0.679125  0.642817  0.697628
.
.

Upvotes: 2

Views: 1118

Answers (1)

jezrael
jezrael

Reputation: 862761

I think need convert values to lists and then call DataFrame contructor:

df = pd.DataFrame({'list':[np.array([1,2,3]), np.array([7,8,3]), np.array([3,7,0])]})
print (df)
        list
0  [1, 2, 3]
1  [7, 8, 3]
2  [3, 7, 0]

df = pd.DataFrame(df['list'].values.tolist(), index=df.index)
print (df)
   0  1  2
0  1  2  3
1  7  8  3
2  3  7  0

And last join to original df:

df = df.join(pd.DataFrame(df['list'].values.tolist(), index=df.index))
print (df)
        list  0  1  2
0  [1, 2, 3]  1  2  3
1  [7, 8, 3]  7  8  3
2  [3, 7, 0]  3  7  0

Another slowier solution is:

df = df.join(df['list'].apply(pd.Series))

Performance:

The plot was created with perfplot:

pic

np.random.seed(57)

def apply(df):
    df = df.join(df['list'].apply(pd.Series))
    return df

def values(df):
    df = df.join(pd.DataFrame(df['list'].values.tolist(), index=df.index))
    return df

def make_df(n):
    df = pd.DataFrame({'list': np.random.randint(10, size=(n, 10)).tolist()})
    return df

perfplot.show(
    setup=make_df,
    kernels=[ apply, values],
    n_range=[2**k for k in range(2, 17)],
    logx=True,
    logy=True,
    equality_check=False,  # rows may appear in different order
    xlabel='len(df)')

Upvotes: 2

Related Questions