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

标签 python pandas dataframe series output-formatting

我从 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/

相关文章:

python - Pandas DataFrame 重新采样中出现意外数量的 bin

python - 从 Pandas 索引中查找整数行索引

python - python中函数字典中的模拟函数

python - 使用 urllib 而不是 http.client 登录网站

python - Selenium 实现 Try except 并写入 JSON

python - 如何在 to_csv 和 read_csv 之后获得一致的数据类型?

python - 将列值与行值匹配并写入新列

python - 在 Python 中以类实例作为参数调用方法

scala - 如何更改数据框列列表的列类型

python - 使用 pandas 合并具有相同列名的两列