1

My input is a pandas dataframe with strings inside:

>>> data
218.0                 
221.0                 
222.0                 
224.0    71,299,77,124
227.0     50,283,81,72
229.0              
231.0           84,349
233.0                 
235.0                 
240.0           53,254
Name: Q25, dtype: object

now i want a shaped ( .reshape(-1,2) ) numpy array of ints for every row like that:

>>> data
218.0                      [] 
221.0                      []
222.0                      []
224.0    [[71,299], [77,124]]
227.0     [[50,283], [81,72]]
229.0                      []
231.0              [[84,349]]
233.0                      []
235.0                      []
240.0              [[53,254]]
Name: Q25, dtype: object

i dont know how to get there by vector operations. can someone help?

2 Answers 2

3

You can use apply, this isn't vector operation though

In [277]: df.val.fillna('').apply(
         lambda x: np.array(x.split(','), dtype=int).reshape(-1, 2) if x else [])
Out[277]:
0                        []
1                        []
2                        []
3    [[71, 299], [77, 124]]
4     [[50, 283], [81, 72]]
5                        []
6               [[84, 349]]
7                        []
8                        []
9               [[53, 254]]
Name: val, dtype: object
Sign up to request clarification or add additional context in comments.

Comments

0

Not very cool, but accurate.

def f(x):
    if x != '':
        x = list(map(int, x.split(',')))
        return list(map(list, zip(x[::2], x[1::2])))
    else:
        return []

s.apply(f)

0                        []
1    [[71, 299], [77, 124]]
2               [[84, 349]]
dtype: object

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.