Populating a Pandas DataFrame frome another DataFrame based on column names Populating a Pandas DataFrame frome another DataFrame based on column names pandas pandas

Populating a Pandas DataFrame frome another DataFrame based on column names


You can just use the list to select them:

In [44]:cols = ['a', 'b', 'b', 'a', 'c']df[cols]Out[44]:   a  b  b  a  c0  1  4  4  1  61  3  2  2  3  42  4  1  1  4  5[3 rows x 5 columns]

So no need for a loop, once you have created your dataframe df then using a list of column names will just index them and create the df you want.


You can do that directly:

>>> df   a  b  c0  1  4  61  3  2  42  4  1  5>>> column_names['a', 'b', 'b', 'a', 'c']>>> df[column_names]   a  b  b  a  c0  1  4  4  1  61  3  2  2  3  42  4  1  1  4  5[3 rows x 5 columns]


From 0.17 onwards you can use reindex like

In [795]: cols = ['a', 'b', 'b', 'a', 'c']In [796]: df.reindex(columns=cols)Out[796]:   a  b  b  a  c0  1  4  4  1  61  3  2  2  3  42  4  1  1  4  5

Note: Ideally, you don't want to have duplicate column names.