Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I need to intersect the dataframe columns with the database columns (defined in a list) before I execute a query.
My current code looks like this:
db_columns_list = ['col1', 'col2', 'col3']
dataframe = dataframe[dataframe.columns & db_columns_list]
This works well, but I am getting the warning:
*FutureWarning: Index.__and__ operating as a set operation is deprecated, in the future this will be a logical operation matching Series.__and__. Use index.intersection(other) instead
dataframe = dataframe[dataframe.columns & db_columns_list]*
So I would like to convert it into something like:
dataframe = dataframe[dataframe.columns.intersection(db_columns_list)]
but with that I get the error:
*Unresolved attribute reference 'intersection' for class 'Iterator'*
Please note that the database may contain more columns than the dataframe.
Therefore I believe the intersection is the correct way to approach this.
Thank you
intersection
function can be applied as below.
Toy example,
db_columns_list = ['col1', 'col2', 'col3']
df=DataFrame({
'col1':[1,2],
'col3':[3,4]
## Solution
df[set(df.columns).intersection(set(db_columns_list))]
# Improved version with the help of `Mustafa Aydın`
df[df.columns.intersection(db_columns_list)]
Output
col3 col1
0 3 1
1 4 2
–
–
–
–
Hope the below code will fix your issue.Intersection works only for sets
common_cols = list(set(dataframe.columns).intersection(set(db_columns_list)))
dataframe = dataframe[common_cols]
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.