Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从中的dataframe获取第2列的最大值,其中第1列中的值=已指定_Python_Pandas - Fatal编程技术网

Python 从中的dataframe获取第2列的最大值,其中第1列中的值=已指定

Python 从中的dataframe获取第2列的最大值,其中第1列中的值=已指定,python,pandas,Python,Pandas,我有一个熊猫数据框。例如: df= paper id year 0 3 1997 1 3 1999 2 3 1999 3 3 1999 4 6 1997 so on 我希望输入与纸张id对应的最长年份。例如,如果给定的纸张id是3,我希望1999作为答案 我该怎么做 有两种通用解决方案-先过滤,然后获取最大值: s = df.loc[df['paper id'] ==

我有一个熊猫数据框。例如:

df=
  paper id  year
0         3  1997
1         3  1999
2         3  1999
3         3  1999
4         6  1997
                so on
我希望输入与纸张id对应的最长年份。例如,如果给定的纸张id是
3
,我希望
1999
作为答案


我该怎么做

有两种通用解决方案-先过滤,然后获取最大值:

s = df.loc[df['paper id'] == 3, 'year'].max()
print (s)
1999

或者将
max
聚合到
系列
,然后选择按
索引
值:

s = df.groupby('paper id')['year'].max()
print (s)
paper id
3    1999
6    1997
Name: year, dtype: int64

print (s[3])
1999

@我想要一个标量输出
s = df.groupby('paper id')['year'].max()
print (s)
paper id
3    1999
6    1997
Name: year, dtype: int64

print (s[3])
1999