gpt4 book ai didi

python - 从数据框或系列的 Pandas 输出中删除名称、数据类型

转载 作者:太空狗 更新时间:2023-10-29 16:55:16 24 4
gpt4 key购买 nike

我从 pandas 函数中得到了这样的输出文件。

Series([], name: column, dtype: object)
311 race
317 gender
Name: column, dtype: object

我正在尝试仅使用第二列获得输出,即

race
gender

通过删除顶部和底部的行,第一列。我该怎么做?

最佳答案

DataFrame/Series.to_string

这些方法有多种参数,允许您配置打印时显示的信息内容和方式。默认Series.to_stringname=Falsedtype=False,所以我们额外指定index=False:

s = pd.Series(['race', 'gender'], index=[311, 317])

print(s.to_string(index=False))
# race
# gender

如果索引很重要,默认值为 index=True:

print(s.to_string())
#311 race
#317 gender

Series.str.cat

当您不关心索引而只想使用 '\n' 左对齐 cat 时。值必须是字符串,因此如有必要,请先进行转换。

#s = s.astype(str)

print(s.str.cat(sep='\n'))
#race
#gender

关于python - 从数据框或系列的 Pandas 输出中删除名称、数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29645153/

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