熊猫-根据A列中的值访问B列中的值

时间:2019-08-28 06:54:48

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

所以我有少量数据,TFR.csv的格式为:

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

我应该确定状态2的值何时处于最低(1994年),并提取状态3在当年的值(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,所以我试图找到一种方法将其从此输出中提取出来。

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

使用pandas.DataFrame.set_indexidxmin

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

输出:

1.4
相关问题