我从 pandas 函数中得到了这样的输出文件。
Series([], name: column, dtype: object)
311 race
317 gender
Name: column, dtype: object
我正在尝试仅使用第二列获得输出,即
race
gender
通过删除顶部和底部的行,第一列。我该怎么做?
最佳答案
DataFrame
/Series.to_string
这些方法有多种参数,允许您配置打印时显示的信息内容和方式。默认Series.to_string
有name=False
和dtype=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/