如何将2d转换为行

时间:2019-07-09 14:41:53

标签: python python-3.x pandas python-2.7

我有一个像这样的数据框:

          a                     b                  
 [[35.6113, -95.855]]    [[[36.028, -95.93], [36.10, -95.82] .... ]]]

我如何将其转换成这样:

      a                     b                  
 [35.6113, -95.855]  [36.028, -95.93]
 [35.6113, -95.855]  [36.10,  -95.82]
  ....                      ....
  ....                      ....

我尝试使用df['b'].apply(pd.Series),但由于它必须为1 d,所以引发了错误。有一个简单的过程可以做到吗?

编辑:

df['b'][0] 

array([[[ 36.0285042 , -95.938618  ],
       [ 36.108158  , -95.8241013 ],
       [ 36.1544989 , -95.9931978 ],
       [ 36.515477  , -96.139459  ],
       [ 36.1330019 , -95.8372588 ],
       [ 36.0405703 , -96.0028027 ],
       [ 35.850413  , -96.072558  ],
       [ 36.0814011 , -95.7677365 ],
       [ 36.40235964, -95.97271572],
       [ 36.0299891 , -95.7085335 ],
       [ 36.328715  , -95.488263  ],
       [ 36.781739  , -95.99514   ],
       [ 36.05102591, -95.72716157],
       [ 35.866565  , -95.523982  ],
       [ 35.8321172 , -96.049276  ],
       [ 35.9691288 , -95.8382827 ],
       [ 36.0872146 , -96.1264932 ],
       [ 35.9682564 , -96.1106048 ],
       [ 36.1375913 , -95.8341317 ],
       [ 35.7914036 , -95.8728959 ]]])

2 个答案:

答案 0 :(得分:3)

检查

s=pd.DataFrame({'a':df.a.repeat(df.b.str.len()),'b':sum(df.b.tolist(),[])})
s.apply(lambda x : x.str[0])

Out[104]: 
                    a                   b
0  [35.6113, -95.855]  [35.6113, -95.855]
0  [35.6113, -95.855]  [35.6113, -95.855]

答案 1 :(得分:0)

如果使用元组不限制您,则可以使用以下方法解决此问题:

s = pd.Series([tuple(x) for x in a])