所以我有一小组数据,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_index
和idxmin
:
df = df.set_index('Year')
df.loc[df['State2'].idxmin(), 'State3']
输出:
1.4
关于python - Pandas - 根据 A 列中的值访问 B 列中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57686557/