gpt4 book ai didi

python - Pandas DataFrame 字符串引用显示选项

转载 作者:太空宇宙 更新时间:2023-11-04 00:56:13 24 4
gpt4 key购买 nike

Pandas DataFrame 在显示字符串时会省略引号。

In [43]: df = pd.DataFrame(np.array([['foo', 'bar', 'bim', 'uncomfortably'],
....: ['horse', 'cow', 'banana', 'apple']]))
....:


In [45]: df
Out[45]:
0 1 2 3
0 foo bar bim uncomfortably
1 horse cow banana apple

如何让它显示引号内的字符串,如下所示?

In [45]: df
Out[45]:
0 1 2 3
0 'foo' 'bar' 'bim' 'uncomfortably'
1 'horse' 'cow' 'banana' 'apple'

我知道我知道

 df[0][0]

返回

'foo' (with quotation marks)

但是,即使我将数据框作为一个整体来调用,我也希望看到引号。

最佳答案

将所有值转换为它们的字符串表示形式:

>>> df.apply(lambda row: [repr(x) for x in row])
0 1 2 3
0 'foo' 'bar' 'bim' 'uncomfortably'
1 'horse' 'cow' 'banana' 'apple'

这相当于:

df.applymap(repr)

如帝斯曼在评论中所建议的那样。

df.apply() 逐行工作,其中 df.applymap() 按元素工作。

关于python - Pandas DataFrame 字符串引用显示选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34967733/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com