Marc Zaharescu Marc Zaharescu - 1 month ago 9
Python Question

Python: form matrix from lists of lists

I have the following data types

A=[1,2,3]
B=[['A', 'C'], ['B', 'C'], ['A', 'B']]
C=[[0 2]
[1 2]
[2 1]]


I want to form a
new_matrix
with the following output

index new_value1 new_value2
1 1 ['A','C'] [0,2]
2 2 ['B','C'] [1,2]
3 3 ['A','B'] [2,1]


where the
index
column has the values from the
A
list,
new_value1
has the values from the
B
list and
new_value2
has the values from the
C
list. The values are in an iterative order.

Any help would be much appreaciated.

Answer

Put your variables in a dictionary, and create a data frame using that dictionary:

a = dict(
    index=[1,2,3],
    new_value1=[['A', 'C'], ['B', 'C'], ['A', 'B']],
    new_value2=[[0, 2], [1, 2], [2, 1]]
)
pd.DataFrame(a)