【问题标题】:How to print out each single unique value in a column如何打印出列中的每个唯一值
【发布时间】:2018-02-07 22:31:21
【问题描述】:

输入:

train['brand_name'].unique()

得到结果:

array([nan, 'Razer', 'Target', ..., 'Astroglide', 'Cumberland Bay',
   'Kids Only'], dtype=object)

我需要查看每个值。有一些由....代表的值。我也想知道如何显示这些值。

谢谢!

【问题讨论】:

标签: python arrays pandas unique


【解决方案1】:

如果您想在 IPython 控制台或 Jupyter 中显示所有行,那么您应该将 pd.options.display.max_rows 设置为不小于您要打印的 Pandas 系列长度的值(如 pd.options.display.max_rows = len(train)),或者您可以将其设置为None。您可以在上下文中执行此操作 --with 允许您的更改是临时的。

with pd.option_context('display.max_rows', None):
    print train['brand_name'].value_counts()
    #or, alternatively
    #print pd.Series(train['brand_name'].unique())

更多关于 Pandas 显示相关选项的信息:​​https://pandas.pydata.org/pandas-docs/stable/options.html

【讨论】:

  • 虽然这篇纯代码帖子可能会回答这个问题,但请添加解释为什么会这样。
【解决方案2】:

这应该可行:

print('\n'.join(map(str, train['brand_name'].unique().tolist())))

解释:

  • \n 代表打印换行符。
  • map(str, lst) 映射到字符串,以防列表中有非文本数据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-30
    • 1970-01-01
    • 2015-12-21
    • 1970-01-01
    • 2022-07-07
    • 2019-07-01
    • 1970-01-01
    相关资源
    最近更新 更多