gpt4 book ai didi

python - 使用我的数据框打印标题

转载 作者:行者123 更新时间:2023-11-30 22:22:43 25 4
gpt4 key购买 nike

如何以下面给出的格式打印我的数据框?

Age      Count
25-29 16
<25 16
30-35 16
>35 16
Name: Age, dtype: int64
Press any key to continue . . .

My Output does not have the headers and here is my code

df.columns=['index','District','Group','Age','Holders','Claims']
df.set_index('index',inplace=True)
df_count=df.Age.value_counts()
print(df_count)

这是我的 csv 的一部分

"","District","Group","Age","Holders","Claims"
"1","1","<1l","<25",197,38
"2","1","<1l","25-29",264,35
"3","1","<1l","30-35",246,20
"4","1","<1l",">35",1680,156
"5","1","1-1.5l","<25",284,63
"6","1","1-1.5l","25-29",536,84
"7","1","1-1.5l","30-35",696,89
"8","1","1-1.5l",">35",3582,400
"9","1","1.5-2l","<25",133,19
"10","1","1.5-2l","25-29",286,52
"11","1","1.5-2l","30-35",355,74
"12","1","1.5-2l",">35",1640,233
"13","1",">2l","<25",24,4
"14","1",">2l","25-29",71,18
"15","1",">2l","30-35",99,19
"16","1",">2l",">35",452,77
"17","2","<1l","<25",85,22
"18","2","<1l","25-29",139,19
"19","2","<1l","30-35",151,22
"20","2","<1l",">35",931,87
"21","2","1-1.5l","<25",149,25
"22","2","1-1.5l","25-29",313,51
"23","2","1-1.5l","30-35",419,49
"24","2","1-1.5l",">35",2443,290
"25","2","1.5-2l","<25",66,14
"26","2","1.5-2l","25-29",175,46
"27","2","1.5-2l","30-35",221,39
"28","2","1.5-2l",">35",1110,143
"29","2",">2l","<25",9,4
"30","2",">2l","25-29",48,15
"31","2",">2l","30-35",72,12
"32","2",">2l",">35",322,53

请帮助我,虽然我得到了值,但我找不到任何东西来获得所需的输出。

PS: i have tried renaming the columns too but its still the same. Thank You.

最佳答案

您需要rename_axisreset_index :

df_count=df.Age.value_counts().rename_axis('Age').reset_index(name='Count')

使用重命名的替代解决方案:

d = {'index':'Age','Age':'Count'}
df_count=df.Age.value_counts().reset_index().rename(columns=d)

关于python - 使用我的数据框打印标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48237795/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com