Python 相当于从表中选择*,其中column1=column2

Python 相当于从表中选择*,其中column1=column2,python,pandas,Python,Pandas,“从表中选择*,其中column1=column2”的等效项是什么 您有一个dataframe,两个带值的列。您需要两列中的数字相同的所有行。代码是什么 dataframe: column1 column2 a b b a c c d d a b a b The result I want: column1 column2 c c d d 谢谢。在这种情况下,您将使用熊猫的

“从表中选择*,其中column1=column2”的等效项是什么

您有一个dataframe,两个带值的列。您需要两列中的数字相同的所有行。代码是什么

dataframe:
column1   column2
a        b
b        a
c        c
d        d
a        b
a        b

The result I want:
column1   column2
c        c
d        d

谢谢。

在这种情况下,您将使用熊猫的一种称为掩蔽的东西

基本上,DataFrame[条件,在一列或整个DataFrame本身上]返回一个条件为真的DataFrame

import pandas as pd
import numpy as np

data = {'a':np.random.randint(0, 10, 100),
       'b':np.random.randint(0, 10, 100)}

df = pd.DataFrame(data)
df[df.a==df.b]

df[df.col1==df.col2]
df.query('col1==col2')
您可能需要阅读df[df.col1==df.col2]工作得非常好,谢谢。非常简单!非常感谢。我认为这是一件非常简单的事情。