Python/Pandas : How to select a cell value, 在同一行中给出 2 个值?

标签 python pandas

我有一个包含 3 列的数据框:

ID     datetime     X
10     01/01/2018   3
10     02/01/2018   4
12     02/01/2018   8
12     07/01/2018   12

现在我的问题是,获取 X 的 ID 和日期的最佳方式是什么?

最佳答案

您可以使用.loc 。来自 documentation :

.loc is primarily label based, but may also be used with a boolean array.

因此,您可以将其用于 bool 索引,并将这两个条件与按位 AND 运算符 & 组合起来。请注意,条件必须用括号分隔。

示例ID日期:

ID = 10
date = '02/01/2018'

数据帧索引:

df.loc[(df.ID == ID) & (df.date == date), 'X']
1    4

关于Python/Pandas : How to select a cell value, 在同一行中给出 2 个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53669669/

相关文章:

用于曲面图的 python xyz 数据转换

python - 在 colaboratory 中从驱动器加载 xlsx 文件

python - 将大型 Pandas DataFrame 写入 SQL Server 数据库

python - 将 pandas DataFrame 从函数中提取到全局变量

python - 使用 Python 复制计算器结果

python - 数据帧上选定值的平均值

python - Pandas 并排堆积条形图

python - 使用 try- except-finally 和上下文管理器增加覆盖范围

python - Setuptools 不为 entry_points 传递参数

python - Spark 中 IF then ELSE 的等价物