Create list of columns to view in data frame

Question:

I have two data frames;

df1 has many columns, lets say a thru to z
df2 contains rows that hold the names of columns I want to expose from df1

df1:

A B C D
1 10 100 1000
2 20 200 2000

df2:

Expose
B
D

df2 will be dynamic and manages in a database table hence the desire to dynamically read the columns to expose instead of hard coding.

I’ve got code that iterates df2 to create the set of columns I’m interested in and returns the string: "[‘B’,’D’]" set to the variable called props

What I want to the do is create a version of df1 but just with the columns B & D.

Trying

df1 = pd.DataFrame(df1, columns: props) 

I get the error that I’m passing a string not a collection which makes perfect sense…. I’m struggling to see how to create the collection to pass through as the columns.

Any help appreciated.

Asked By: reddwarfcrew

||

Answers:

Damn, no soon as I post this I find the answer. Instead of creating the string I create a list and use props.append() when creating the list from df2.

Answered By: reddwarfcrew

Try:

to get columns names from df2:

list_of_col =  df2['Expose'].tolist()

above will return a list of columns in df2

now use above list in below code line:

new_df = df1.loc[:, df1.columns.intersection(list_of_col)] 
print(new_df .columns)

this will give you required dataframe.

Answered By: Mohit

You do it all in one line:

df1 = df1[df2['Expose']]
Answered By: user19077881
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.