如何使用pandas显示系列的所有元素

huangapple go评论81阅读模式
英文:

How to show all elements of a series using pandas

问题

I have a list containing strings that I want to separate with a '|' symbol. The problem I am facing is that creating a pandas series only shows the first few observations followed by ... Is there something I can do to avoid that? Thanks

英文:

I have a list containing strings that I want to separate with a '|'. The problem I am facing is that creating a pandas series only shows the first few observations followed by ... Is there something I can do to avoid that? Thanks

import pandas as pd

names = pd.Series([['james', 'han', 'sam', 'john', 'dan',
                    'greg', 'adam', 'ben', 'johan', 'asesh', 'kofi']])
name_list = names.str.join('|').to_string(index=False)

如何使用pandas显示系列的所有元素

答案1

得分: 1

这个答案讨论了Pandas如何自动截断长字符串,所以你的names_list没有打印出来,因为它已经被截断了。设置下面的Pandas参数来解决这个特定的问题:

import pandas as pd
pd.options.display.max_colwidth = None

names = pd.Series([['james', 'han', 'sam', 'john', 'dan',
                    'greg', 'adam', 'ben', 'johan', 'asesh', 'kofi']])
name_list = names.str.join('|').to_string(index=False)
print(name_list)

james|han|sam|john|dan|greg|adam|ben|johan|asesh|kofi
英文:

This answer discusses how Pandas automatically truncates long strings, so your names_list isn't printing because it's been truncated. Set the Pandas parameter below to fix this particular issue:

import pandas as pd
pd.options.display.max_colwidth = None

names = pd.Series([['james', 'han', 'sam', 'john', 'dan',
                    'greg', 'adam', 'ben', 'johan', 'asesh', 'kofi']])
name_list = names.str.join('|').to_string(index=False)
print(name_list)

james|han|sam|john|dan|greg|adam|ben|johan|asesh|kofi

答案2

得分: 0

你可以使用 to_dict() 或 to_list()。

输出:

{0: ['james', 'han', 'sam', 'john', 'dan', 'greg', 'adam', 'ben', 'johan', 'asesh', 'kofi']}

[['james', 'han', 'sam', 'john', 'dan', 'greg', 'adam', 'ben', 'johan', 'asesh', 'kofi']]

英文:

You can use to_dict() or to_list().

Output:

{0: ['james',
  'han',
  'sam',
  'john',
  'dan',
  'greg',
  'adam',
  'ben',
  'johan',
  'asesh',
  'kofi']}

Or

[['james',
  'han',
  'sam',
  'john',
  'dan',
  'greg',
  'adam',
  'ben',
  'johan',
  'asesh',
  'kofi']]

huangapple
  • 本文由 发表于 2023年6月15日 10:47:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76478758.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定