0

Trying to convert a python list to a pandas dataframe:

input:
list = [[a,x,[1,2]],[a,y,[1]],[a,z,[1,2,3]],[b,v,[1]],[b,w,[1,2]]...]

some conversion trickery to result in:

output:
c1 c2 c3
 a  x  1
 a  x  2
 a  y  1
 a  z  1
 a  z  2
 a  z  3
 b  v  1
 b  w  1
 b  w  2

The first and second elements of each list element are always single values (that become c1 and c2), but the third element is a list of varying length. I tried a variety of loops to convert to a list with each element ready as a 'row' but I couldn't make it work. Its kind of like an inverse pivot table but that didn't really help me either. Any help is appreciated

1 Answers1

0
enter code here
import pandas as pd
import numpy as np

df = pd.DataFrame([['a','x',[1,2]],['a','y',[1]],['a','z',[1,2,3]],['b','v',[1]],['b','w',[1,2]]],columns=["colum1","column2","column3"])

print(df)

print("******************")




lst_col = 'column3'

r = pd.DataFrame({
  col:np.repeat(df[col].values, df[lst_col].str.len())
  for col in df.columns.drop(lst_col)}
).assign(**{lst_col:np.concatenate(df[lst_col].values)})[df.columns]
print(r)

enter image description here

Ravi
  • 2,778
  • 2
  • 20
  • 32