【问题标题】:Issue with choosing columns when creating max/min columns in dataframe在数据框中创建最大/最小列时选择列的问题
【发布时间】:2014-08-09 00:07:47
【问题描述】:

我有以下代码:

def minmaxdata():
    Totrigs,TotOrDV,TotOrH,TotGas,TotOil,TotBit,ABFl,ABOr,SKFl,SKOr,BCOr,MBFl,MBOr = dataforgraphs()
    tr = Totrigs
    tr['year'] = tr.index.year
    tr['week']= tr.groupby('year').cumcount()+1
    tr2 = tr.pivot_table(index='week',columns='year')
    tr2['max07_13']=tr2.max(axis=1)   
    tr2['min07_13']=tr2.min(axis=1)

    print(tr2)

这给了我这个:

      Total Rigs                                            max07_13  min07_13
year        2007  2008  2009  2010  2011  2012  2013  2014                    
week                                                                          
1            408   333   303   322   419   382   270   477       477       270
2            539   449   357   382   495   541   460   514       541       357
3            581   482   355   419   511   554   502   509       581       355
4            597   485   356   441   514   568   502   502       597       356
5            587   496   340   462   522   570   503   500       587       340
6            590   521   304   457   526   564   506   512       590       304
7            586   539   294   465   517   571   519   530       586       294
8            555   529   282   455   517   555   517   NaN       555       282
9            550   534   232   437   532   519   518   NaN       550       232
10           510   502   160   366   528   419   472   NaN       528       160
11           396   411   107   259   466   296   405   NaN       466       107

...但是我希望右侧的两个最大/最小列只取 2007-2013 年的最大/最小。我尝试了几种索引方法,但似乎会导致错误。

有什么建议吗??

编辑:

尝试了可扩展的解决方案,但出现以下错误:

KeyError: "['2007' '2008' '2009' '2010' '2011' '2012' '2013'] not in index"

EDIT2:

tr2.columns 输出如下:

            Year
Total Rigs  2007
            2008
            2009
            2010
            2011
            2012
            2013
            2014
 max07_13        
 min07_13 

EDIT3:

这是解决方案:

    gcols=[('Total Rigs',2007),('Total Rigs',2008),('Total Rigs',2009),('Total Rigs',2010),('Total Rigs',2011),('Total Rigs',2012),('Total Rigs',2013)]
    tr2['Max 2007-2013']=tr2[gcols].max(axis=1)   
    tr2['Min 2007-2013']=tr2[gcols].min(axis=1)

【问题讨论】:

    标签: python pandas max dataframe min


    【解决方案1】:

    一个不可扩展的解决方案是drop 2014 然后调用maxmin -

    tr2['max07_13']=tr2.drop('2014', axis=1).max(axis=1)
    

    如果您知道感兴趣的列,您也可以使用它 -

    columns_of_interest = ['2007', '2008', '2009', '2010', '2011', '2012', '2013']
    tr2['max07_13']=tr2[columns_of_interest].max(axis=1)
    

    【讨论】:

    • 见上面的编辑。另外,感谢您对此的帮助。
    • @carevans88 ,列可能不是字符串。你可以试试 - columns_of_interest = [2007, 2008, 2009, 2010, 2011, 2012, 2013]
    • 看来您可能有多级列。如果是这样,您可以发布tr2.columns的输出吗?
    • 好像是多级的,所以可以使用columns_of_interest = [('Total Rigs', 2007), ('Total Rigs', 2008), ('Total Rigs', 2009), ('Total Rigs', 2010), ('Total Rigs', 2011), ('Total Rigs', 2012), ('Total Rigs', 2013)]
    猜你喜欢
    • 2020-09-09
    • 1970-01-01
    • 1970-01-01
    • 2017-11-12
    • 2011-01-27
    • 1970-01-01
    • 2022-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多