python - Pandas - 根据 A 列中的值访问 B 列中的值

标签 python python-3.x pandas dataframe jupyter-notebook

所以我有一小组数据,TFR.csv 采用以下形式:

Year     State1     State2     State3
1993       3          4           5
1994       6          2           1.4
...

我应该确定州 2 的值(value)何时达到最低(1994 年),并提取州 3 在该年的值(value) (1.4)。

为此,我编写了一个简短的过滤器:

State1Min = min(TFR['State1']) #Determine the minimum value for State1

filt = (TFR['State1']==State1Min) #Filter to select the row where the value exists

TFR[filt]['State3'] #Apply this filter to the original table, and return the value in the State3 column.

它返回我正在寻找的正确值,但也返回前面的行号:

2     1.4
Name: NT, dtype: float64

我需要打印这个值 1.4,所以我试图找到一种方法从这个输出中提取它。

感谢您的帮助。

最佳答案

使用pandas.DataFrame.set_indexidxmin:

df = df.set_index('Year')
df.loc[df['State2'].idxmin(), 'State3']

输出:

1.4

关于python - Pandas - 根据 A 列中的值访问 B 列中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57686557/

相关文章:

java - 尝试解析数据包: Is there a Java equivalent to Python's "unpack"?

python-3.x - 如何使用python在keras中输入图像进行类预测

python - 如何对不同类型的列表进行排序?

pandas - graphlab 查找至少具有一个 None 值的所有列

python - 将 pandas DataFrame 中的列转换为具有 nan 值的 float

python - 使用 MultiIndex 沿多个维度对 Pandas 系列进行切片的有效方法是什么?

python - 用于大型(r)数据集的 Pandas

python - 我的代码以错误的顺序给我数字

Python:WAITING用户输入,如果10分钟后没有输入,则继续执行程序

Python:构建后出现 CX_Freeze 问题