1

I have two lists. I have a list of table titles(title_df). My other list is from my contents (prediction_df) to be sorted by titles. I want to populate my contents by titles and create a table in the result.

title_df=['a','b']

prediction_df=['1','2','3','800800','802100','800905']

My table has three rows and two columns

2
  • Hi, what were your attempts and where did they fail at (if at all)? I see you accepted an answer but do you really understand what's going on there (e.g., why is order="F" needed)? If you shared your attempts, maybe the answerers could have pointed what's missing and/or have built their answers on top of it., leading to a better understanding. Commented Jan 10, 2022 at 8:30
  • Hi, stackoverflow.com/questions/45973722/… solved the details from this link. The question I asked contains only a small part of the problem at hand. I am trying to carry out a different study based on the answer. Commented Jan 10, 2022 at 10:16

1 Answer 1

2

Use numpy.reshape, 2 is for 2 columns and -1 is for count number of rows by data, last pass to DataFrame constructor:

df = pd.DataFrame(np.reshape(prediction_df, (-1,2), order='F'), columns=title_df)
print (df)
   a       b
0  1  800800
1  2  802100
2  3  800905
Sign up to request clarification or add additional context in comments.

3 Comments

thanks for your answer. If prediction_df= [[['1','2','3']],[['800800','802100','800905']]], how would my management change?
@ysmnrn.dll - Then use df = pd.DataFrame([x[0] for x in prediction_df], index=title_df).T
@ysmnrn.dll - if prediction_df= [['1','2','3'],['800800','802100','800905']] then df = pd.DataFrame(prediction_df, index=title_df).T

Your Answer

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