我有一個名為df_out的df,在下面的插入中,列名是這樣的,但是由于某些原因,我不能對列頭使用'groupby'功能,因為它一直給我KeyError: 'year'。我已經研究并嘗試了去除空白,重設索引,允許在我的groupby設定前有空白,等等,但我無法克服這個KeyError。df_out看起來是這樣的:
df_out.columns
Out[185]。
Index(['year', 'month', 'BARTON CHAPEL', 'BARTON I', 'BIG HORN I',
'BLUE CREEK', 'BUFFALO RIDGE I', 'CAYUGA RIDGE', 'COLORADO GREEN',
'DESERT WIND', 'DRY LAKE I', 'EL CABO', 'GROTON', 'NEW HARVEST',
'PENASCAL I'/span>, 'RUGBY'/span>, 'TULE'/span>]。
dtype='object', name='plant_name')
但是,當我使用df_out.head()時,我得到了一個不同的答案,它的前導列是'plant_name',所以這也許是錯誤的來源或相關。下面是來自-
的輸出列df_out.head()
Out[187]。
plant_name year month BARTON CHAPEL BARTON I BIG HORN I BLUE CREEK
0 1991 1 6. 432285 7.324126 5.170067 6.736384
1 1991 2 7. 121324 6.973586 4.922693 7.473527 2 1991 3 8. 125793 8.681317 5.796599 8.401855
3 1991 4 7. 454972 8.037764 7.272292 7.9616254 1991 5 7. 012809 6.530013 6.626949 6.009825
plant_name BUFFALO RIDGE I CAYUGA RIDGE COLORADO GREEN DESERT WIND
0 7.163790 7.145323 5.783629 5.682003
1 7.595744 7.724717 6.245952 6.269524[/span
2 8.111411 9.626075 7.918871 6.657648[/span
3 8.807458 8.618806 7.011444 5.848736[/span
4 7.734852 6.267097 7.410013 5.099610
plant_name DRY LAKE I EL CABO GROTON NEW HARVEST PENASCAL I
0 4.721089 10. 747285 7.456640 6.921801 6.296425[/span
1 5.095923 8. 891057 7.239762 7.449122 6.484241[/span
2 8.409637 12. 238508 8.274046 8.824758 8.444960
3 7.893694 10. 837139 6.381736 8.840431 7.282444
4 8.496976 8. 636882 6.856747 7.469825 7.999530
植物名稱 RUGBY TULE
0 7.028360 4.110605[/span
1 6.394687 5.257128[/span]。
2 6.859462 10.789516[/span]。
3 7.590153 7.425153[/span].
4 7.556546 8.085255
我的得到KeyError的groupby陳述句是這樣的,我試圖根據串列中找到的df_out的列子集--'west',按年和月的行來計算平均數:
我的groupby陳述句是這樣的。
west=['BIG HORN I', 'DRY LAKE I', 'TULE']
westavg = df_out[df_out.columns[df_out.columns.isin(west)]].groupby(['year','month']) .mean()
非常感謝你,
uj5u.com熱心網友回復:
你的代碼可以分解成:
westavg = (df_out[df_out.columns[df_out.columns.isin(west)])
.groupby(['year','month']) .mean()
)
這是不作業的,因為['year','month']不是df_out[df_out.columns[df_out.columns.isin(west)]]的列。
嘗試:
west_cols = [c for c in df_out if c in west]
westavg = df_out.groupby(['year','month']) [west_cols].mean()
uj5u.com熱心網友回復:
好的,在下面Quang Hoang的幫助下,我理解了這個問題,并得出了這個答案,我能夠使用.intersection更好地理解這個答案:
westavg = df_out[df_out.columns.intersection(west)].mean( axis=1)
#給我提供了由串列'west'定義的列子集的每一行的平均值。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/332422.html
標籤:
