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

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
  • 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. –  Jan 10 '22 at 08:30
  • Hi, https://stackoverflow.com/questions/45973722/how-does-numpy-reshape-with-order-f-workI 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. –  Jan 10 '22 at 10:16

1 Answers1

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
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • thanks for your answer. If prediction_df= [[['1','2','3']],[['800800','802100','800905']]], how would my management change? –  Jan 10 '22 at 07:30
  • @ysmnrn.dll - Then use `df = pd.DataFrame([x[0] for x in prediction_df], index=title_df).T` – jezrael Jan 10 '22 at 07:33
  • @ysmnrn.dll - if `prediction_df= [['1','2','3'],['800800','802100','800905']]` then `df = pd.DataFrame(prediction_df, index=title_df).T` – jezrael Jan 10 '22 at 07:34