python - 查找匹配 2 列条件的单个数据帧行索引

标签 python pandas numpy dataframe

我有一个包含 3 列的数据框,我需要获取与 2 列的值匹配的行的索引。

例如,下面的数据框:

Name     City   Country
Pietro   Roma   Italy
Maria    Milan  Italy
Pietro   NY     USA

在这种情况下,我需要获取 Pietro|Roma|Italy 的索引,仅搜索名称和城市列。

我尝试执行下面的代码,但它返回了与 2 列匹配的所有行。

idx = np.where(dataframe[dataframe["Name"] == 'Pietro'],dataframe[dataframe["City"] == 'Roma'])

但它返回一个索引数组 [[0,2],[0]],我需要返回索引 0,即 Name = 'Pietro' 和 City = 'Roma'

更新了解决方案

解决方法是:

dataframe.index[(dataframe["Name"] == 'Pietro')&(dataframe["City"] == 'Roma')][0]

最佳答案

使用

dataframe.index[(dataframe["Name"] == 'Pietro')&(dataframe["City"] == 'Roma')]

关于python - 查找匹配 2 列条件的单个数据帧行索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54873393/

相关文章:

python - 用 Pandas 快速删除标点符号

python - 创建自定义图像数据集时出现 numpy 数组形状问题

python - Numpy---如何同时替换数组中的某些多个元素?

python - 在 SQLalchemy 中过滤左连接

python - SQLalchemy Core,检索更新行的 ID

python - 如何在 Pandas DataFrame 中查找包含数字的字符串数据类型

python - 使用 numpy where : for black cell, 将新值放入列中

python - pyopengl - 动态更新顶点缓冲区对象中的值

python - PyCharm Idea 中的 Jenkinsfile 语法高亮显示

javascript - 将内容推送到浏览器...更改 DOM?