convert list values to rows in pandas

≡放荡痞女 提交于 2019-12-12 17:24:08

问题


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:


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:

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)')


来源:https://stackoverflow.com/questions/51282642/convert-list-values-to-rows-in-pandas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!