Python DataFrame-基于同一DataFrame列中的值选择DataFrame行
我正在努力解决与数据帧相关的问题Python DataFrame-基于同一DataFrame列中的值选择DataFrame行,python,pandas,dataframe,Python,Pandas,Dataframe,我正在努力解决与数据帧相关的问题 columns = [desc[0] for desc in cursor.description] data = cursor.fetchall() df2 = pd.DataFrame(list(data), columns=columns) df2如下所示: | Col1 | Col2 | | -------- | -------------- | | 2145779 | 2 | | 8059234
columns = [desc[0] for desc in cursor.description]
data = cursor.fetchall()
df2 = pd.DataFrame(list(data), columns=columns)
df2如下所示:
| Col1 | Col2 |
| -------- | -------------- |
| 2145779 | 2 |
| 8059234 | 3 |
| 2145779 | 3 |
| 4265093 | 2 |
| 2145779 | 2 |
| 1728234 | 5 |
我想列出col1中的值,其中col2=“3”的值可以使用布尔索引:
out=df2.loc[df2.Col2.eq(3),“Col1”].agg(列表)
打印(输出)
印刷品:
[8059234,2145779]
您可以使用布尔索引:
out=df2.loc[df2.Col2.eq(3),“Col1”].agg(列表)
打印(输出)
印刷品:
[8059234,2145779]