英文:
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)
答案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']]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论