使用 Pandas,为什么列/系列标题没有显示在输出的顶部 [英] Using Pandas, why is the column/series header not showing at the top of an output
问题描述
在:
def answer_three():
Top15 = answer_one()
#https://stackoverflow.com/questions/51826751/average-of-dataframe-columns
Top15['avgGDP'] = Top15[['2006', '2007', '2008', '2009', '2010', '2011', '2012', '2013', '2014', '2015']].mean(axis=1)
Top15 = Top15.sort_values('avgGDP', ascending=False)
return Top15['avgGDP']
answer_three()
出:
Country
UU 15368
CF 6348
J 5542
G 3493
F 2681
UT 2487
B 2189
IA 2120
IB 1769
CG 1660
R 1565
S 1418
A 1164
S 1106
T 7704
Name: avgGDP, dtype: float64
观察:
标题/系列名称与数据类型一起显示在底部.我已经研究过但找不到答案,只添加/重命名标题,只提取标题等.这不是我的问题,我只是希望输出在我正常调用数据帧时看起来,但只有这个选择/单一字段在这种情况下,即我想要:
The header/series name shows at the bottom along with the datatype. I have researched and can't find the answer, only adding/renaming headers, extracting only headers, etc. That is not my problem, I simply want the output to look when I call a dataframe normally, but only this select/singular field in this case, i.e. i want:
Country avgGDP
UU 15368
CF 6348
J 5542
G 3493
F 2681
UT 2487
B 2189
IA 2120
IB 1769
CG 1660
R 1565
S 1418
A 1164
S 1106
T 7704
我在输出全帧之间看到的唯一不同之处在于我请求的是单个特定列.我假设我必须在我的 return 语句中添加一些东西,所以它输出就像我调用了一个完整的数据框.
The only thing I see different between output a full frame and this is that I am requesting a single specific column. I am assuming I have to add something to my return statement, so it output like I called a full dataframe.
非常感谢您的时间和帮助!
I appreciate your time and assistance in advance!
谢谢,
马特
推荐答案
系列名称始终显示在系列下方.DataFrame 列名称显示在列上方.您可以将系列转换为单列 DataFrame:
A Series name is always shown under the Series. A DataFrame column name is shown above the column. You can convert the Series into a one-column DataFrame:
pd.DataFrame(Top15['avgGDP'])
它将按照您的预期显示.
It will be displayed as you expect.
这篇关于使用 Pandas,为什么列/系列标题没有显示在输出的顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!