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
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.
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.
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.
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.
You do it all in one line:
df1 = df1[df2['Expose']]