I have created a dataframe "df" which looks like this:
Name
0. School
1. Organisation
2. Teacher
3. Guest
now I have three lists
1. A = ['','','',['12','4']]
2. B = ['','','',['3','8']]
3. status = ['','','','[['yes','no','yes'],['no','yes','no']]]
4. letter = ['', '', '', [[['K', 'L'], ['L'], ['L']], [['O'], ['P', 'O'], ['K']]]]
I want to add these 4 lists to my existing dataframe df and it should look like this
Name A1 A2 status letter
0. School
1. Organisation
2. Teacher
3. Guest 12 3 yes K
3. Guest 12 3 yes L
3. Guest 12 3 no L
3. Guest 12 3 yes L
3. Guest 4 8 no O
3. Guest 4 8 yes P
3. Guest 4 8 yes O
3. Guest 4 8 no K
I have tried df['from']=from and df['to']=to
But this didn't give me table as I expected.
I have tried this as well:
dfa=pd.DataFrame({"Names":names})
def flat(nums):
res = []
index = []
for i in range(len(nums)):
if isinstance(nums[i], list):
res.extend(nums[i])
index.extend([i]*len(nums[i]))
else:
res.append(nums[i])
index.append(i)
return res,index
x="A1"
list_flat,list_index=flat(eval(x))
dataframe = pd.DataFrame({x:list_flat},index=list_index)
df = pd.concat([df['Names'],dataframe],axis=1,sort=False)
x="A2"
list_flat,list_index=flat(eval(x))
dataframe = pd.DataFrame({x:list_flat},index=list_index)
df= pd.concat([df,dataframe],axis=1,sort=False)
x="status"
list_flat,list_index=flat(eval(x))
dataframe = pd.DataFrame({x:list_flat},index=list_index)
df = pd.concat([df,dataframe],axis=1,sort=False)
x="letter"
list_flat,list_index=flat(eval(x))
dataframe = pd.DataFrame({x:list_flat},index=list_index)
df = pd.concat([df,dataframe],axis=1,sort=False)
But instead of appearing on different rows nested lists are appearing on same row like this
Name A1 A2 status letter
0. School
1. Organisation
2. Teacher
3. Guest 12 3 ['yes','no','yes'] [['K','L'],['L'],['L']]
3. Guest 4 8 ['no','yes','no'] [['O'],['P','O'],['K']]
letter2-dimensional