Select columns of dataframe with a list, python

Keywords: python mysql

Question: 

I do a query to mySql and I form a dataframe df, which has 100 columns with differents type of animals.

connection = pymysql.connect(...)
query = ..
animals = pd.read_sql(query, connection)

The above is a table of values, so I convert this table to dataframe with pivot

df=animals.pivot(index='ID', columns='type_animal', values='count').fillna(0) df=pd.DataFrame(df.to_records())

where Id, is an id for animal and count is a number 1 or 0. This give me something like,

ID dog cat cow chicken ... elephant

0 0 1 0 0 0

1 1 0 0 0 0

Then I do another query to mySql, to have the type of the animals from the ultimate two months, wich is a table(named 'table_names') of 64 index, i.e.,

Type_animal

0 lion

1 raccon

64 cow

My problem is: How can select the columns of the data frame df, using the table with the type of the animals?

I know this is very simple, since is enough df_new=df['lion',...,'cow'] but for an unkown reason, I think doesn't work the statement df_new=df[table_names] o df_new=df[list(table_names)]. Anyway, I tried to create a new dataframe with 'table_names', but is just a table of 1 x 64...

Any hint will be appreciated. Thanks!

Answers: