Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.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 如何查找列的名称和最大值_Python_Python 3.x_Pandas - Fatal编程技术网

Python 如何查找列的名称和最大值

Python 如何查找列的名称和最大值,python,python-3.x,pandas,Python,Python 3.x,Pandas,我有datadf_movies2,列有:年份、制作公司和该特定年份产生的收入。我想返回每年,什么是最大的收入以及生产公司的名称。例如,2016年,Babelsberg工作室的收入最高 这是我试过的 import pandas as pd df_movie2.groupby(['year','production_companies']).revenue.max() 但是,每年都要返回所有制作公司的名称是行不通的。 谢谢你的帮助我不太确定你希望得到什么回报。如果您的输出按您的需要排序,但缺

我有datadf_movies2,列有:年份、制作公司和该特定年份产生的收入。我想返回每年,什么是最大的收入以及生产公司的名称。例如,2016年,Babelsberg工作室的收入最高

这是我试过的

  import pandas as pd 
df_movie2.groupby(['year','production_companies']).revenue.max()
但是,每年都要返回所有制作公司的名称是行不通的。
谢谢你的帮助

我不太确定你希望得到什么回报。如果您的输出按您的需要排序,但缺少值,那是因为.max正在删除您各自年份的重复项。请参见编辑1,以从最大值到最小值的升序返回所有值

如果这是一个排序问题,您希望将最大值返回到最小值,并且不担心每年删除重复的生产公司,请参阅编辑2:

import pandas as pd

d = ({
    'year' : ['2016','2016','2016','2016','2016','2015','2015','2015','2015','2014','2014','2014','2014'],        
    'production_companies' : ['Walt Disney Pictures','Universal Pictures','DC Comics','Twentieth Century','Studio Babelsberg','DC Comics','Twentieth Century','Twentieth Century','Universal Pictures','The Kennedy/Marshall Company','Twentieth Century','Village Roadshow Pictures','Columbia Pictures'],                 
    'revenue' : [966,875,873,783,1153,745,543,521,433,415,389,356,349],                                     
     })

df = pd.DataFrame(data = d)
编辑1:

df = df.sort_values(['revenue', 'year'], ascending=[0, 1])
df = df.set_index(['year', 'production_companies'])
输出:

                                   revenue
year production_companies                 
2016 Studio Babelsberg                1153
     Walt Disney Pictures              966
     Universal Pictures                875
     DC Comics                         873
     Twentieth Century                 783
2015 DC Comics                         745
     Twentieth Century                 543
     Twentieth Century                 521
     Universal Pictures                433
2014 Twentieth Century                 389
     Village Roadshow Pictures         356
     Columbia Pictures                 349
     The Kennedy/Marshall Company      320
                                   revenue
year production_companies                 
2016 Studio Babelsberg                1153
     Walt Disney Pictures              966
     Universal Pictures                875
     DC Comics                         873
     Twentieth Century                 783
2015 DC Comics                         745
     Twentieth Century                 543
     Universal Pictures                433
2014 Twentieth Century                 389
     Village Roadshow Pictures         356
     Columbia Pictures                 349
     The Kennedy/Marshall Company      320
编辑2:

df = df.groupby(['year','production_companies'])[['revenue']].max()
idx = df['revenue'].max(level=0).sort_values().index
i = pd.CategoricalIndex(df.index.get_level_values(0), ordered=True, categories=idx)
df.index = [i, df.index.get_level_values(1)]
df = df.sort_values(['year','revenue'], ascending=False)
输出:

                                   revenue
year production_companies                 
2016 Studio Babelsberg                1153
     Walt Disney Pictures              966
     Universal Pictures                875
     DC Comics                         873
     Twentieth Century                 783
2015 DC Comics                         745
     Twentieth Century                 543
     Twentieth Century                 521
     Universal Pictures                433
2014 Twentieth Century                 389
     Village Roadshow Pictures         356
     Columbia Pictures                 349
     The Kennedy/Marshall Company      320
                                   revenue
year production_companies                 
2016 Studio Babelsberg                1153
     Walt Disney Pictures              966
     Universal Pictures                875
     DC Comics                         873
     Twentieth Century                 783
2015 DC Comics                         745
     Twentieth Century                 543
     Universal Pictures                433
2014 Twentieth Century                 389
     Village Roadshow Pictures         356
     Columbia Pictures                 349
     The Kennedy/Marshall Company      320

请查看groupbycolumn_name.revenue.maxI尝试了此操作,但无效,返回了所有制作公司的名称。下面是代码:df_movie2.groupby['year','production_companys'].revenue.max请在您的问题中提供答案,包括您迄今为止尝试了什么以及为什么它不起作用max而不是max