2

I have a nested list like this

a=[ [[12,23],[31,41]], [[53,68],[77,87]] ]

and I'd like to create a pandas data frame like this

id1  id2   c2   c3
1     1    12   31
1     2    53   77
2     1    23   41
2     2    68   87

My idea was to do this:

ids = {'id1':[1,1,2,2],'id2':[1,2,1,2]}
df1 = pd.DataFrame(ids)
L = [[t[idx] for t in l] for idx in [0,1] for l in a]
df2 = pd.DataFrame(L, columns=['c1','c2'])
pd.concat([df1,df2], axis=1)

Is there a way to achieve the same result without so many commands?

4
  • Show your work and research. What happens if you just do it directly? Commented Mar 22, 2018 at 20:39
  • I don't understand how you generate id1 and id2. Please explain @HappyPy Commented Mar 22, 2018 at 20:59
  • @Mad Physicist: I've edited my question Commented Mar 22, 2018 at 21:23
  • @Manish Saraswat: please see my edits Commented Mar 22, 2018 at 21:25

1 Answer 1

2
from itertools import product

# generate id1 and id2 using `itertools.product`
v = np.arange(len(a))    
i = pd.DataFrame(np.array(list(product(v, v))) + 1)
# concatenate `a` along the 0th axis
j = pd.DataFrame(np.concatenate(a, axis=0))

# concatenate `i` and `j`
df = pd.concat([i, j], axis=1)
# set the column names
df.columns = ['id1', 'id2', 'c2', 'c3']

   id1  id2  c2  c3
0    1    1  12  23
1    1    2  31  41
2    2    1  53  68
3    2    2  77  87
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.