No_body
No_body

Reputation: 842

How to convert 2d into row

I have a data-frame like:

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

How can i convert it into like this:

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

I tried with df['b'].apply(pd.Series) but it's throwing error as it must be 1 d. Is there a simple process to do it?

Edit:

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

Upvotes: 0

Views: 82

Answers (2)

alexandre_d
alexandre_d

Reputation: 155

If using tuple doesn't limit you, you can solve this with:

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

Upvotes: 0

BENY
BENY

Reputation: 323226

Check with

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]

Upvotes: 3

Related Questions