2017-05-29 283 views
1

我工作的Python 3.4和我有一個包含數據框大熊貓柱:拆分大熊貓列蟒蛇

0 [0.3785766661167145, -0.449486643075943, -0.15...] 
1 [0.204025000333786, -0.3685399889945984, 0.231...] 
2 [0.684576690196991, -0.5823000073432922, 0.269...] 
3 [-0.02300500124692917, -0.22056499123573303, 0...] 
Name: comments, dtype: object 

,我想拆它,把它變成multople列:

column1    column2    ...columnx 
0 0.3785766661167145 -0.449486643075943  last element in the first list 
1 0.204025000333786  -0.3685399889945984 last element in the 2nd list 
2 0.684576690196991  -0.5823000073432922 last element in the 3rd list 
3 -0.02300500124692917 -0.22056499123573303 last element in the 4th list 

你能幫我嗎? 在此先感謝

回答

0

^h AVING一個數據幀

df = pd.Series(
    {'comments': [list(np.random.randn(3).round()) for i in range(4)] 
    } 
) 

其中df=

comments 
0 [1.0, -2.0, 0.0] 
1 [1.0, -3.0, -0.0] 
2 [-0.0, -0.0, -1.0] 
3 [-2.0, -2.0, -2.0] 

調用

df2 = DataFrame(list(df['comments'])) 

你獲得

 0 1 2 
0 1.0 -2.0 0.0 
1 1.0 -3.0 -0.0 
2 -0.0 -0.0 -1.0 
3 -2.0 -2.0 -2.0 
0

測試用例:

import pandas as pd 
df = pd.DataFrame({ 
       'var1':['20, -20, -50','30, 20, -50','40','30'], 
       'var2':['10','50','60','70'] 
       }) 
print(df) 

    var1   var2 
0 20, -20, -50 10 
1 30, 20, -50  50 
2 40    60 
3 30    70 

pd.concat([df[['var2']], df['var1'].str.split(',', expand=True)], axis=1) 

enter image description here

1

如果數據是lists需要DataFrame構造函數轉換列commentsnumpy arrayvalues + tolist

print (type(df.loc[0, 'comments'])) 
<class 'list'> 

df1 = pd.DataFrame(df['comments'].values.tolist()) 
#rename columns if necessary 
df1 = df1.rename(columns = lambda x: 'column' + str(x + 1)) 
print (df1) 
    column1 column2 column3 
0 0.378577 -0.449487 -0.150 
1 0.204025 -0.368540 0.231 
2 0.684577 -0.582300 0.269 
3 -0.023005 -0.220565 0.000 
0

使用@ dDanny的例子據幀,

df = pd.DataFrame(
    {'comments': [list(np.random.randn(3).round()) for i in range(4)] 
    }) 

您可以使用apply將包含列表的列轉換爲Dataframe。

df.comments.apply(pd.Series) 
Out[127]: 
    0 1 2 
0 -2.0 -3.0 -1.0 
1 1.0 0.0 1.0 
2 -1.0 -1.0 -0.0 
3 1.0 1.0 0.0