添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
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
                pd.Index has an intersection method and works without casting to set. Your example works without both sets. I think the issue is different
– Mustafa Aydın
                May 9, 2021 at 7:23
                pd.Index has all the functionality of the set plus more for example - It's ordered and it can contain duplicates as well. So, I guess the problem is diff. here as suggested by @MustafaAydın
– Nk03
                May 9, 2021 at 7:27
                The new solution proposed: df[df.columns.intersection(db_columns_list)]   for me does not work. I get: Unresolved attribute reference 'intersection' for class 'Iterator'.  The previous one with set worked.
– Forna
                May 9, 2021 at 7:28
                nw, Thanks for +1, We are here to help each other. It's okay if someone  downvotes(his choice).
– Utsav
                May 9, 2021 at 7:46

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.