2

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
.
.
1

1 Answer 1

2

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)')
Sign up to request clarification or add additional context in comments.

1 Comment

but can we able to add it in the same dataframe? i have few other columns in df that i will be needing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.