gpt4 book ai didi

Python 漂亮的 get_string 错误

转载 作者:行者123 更新时间:2023-12-01 09:18:25 25 4
gpt4 key购买 nike

我有一个像这样的 pandas 数据框

>>> df
Out[126]:
score id
0 0.999989 654153
1 0.992971 941351
2 0.979518 701608
3 0.972667 564000
4 0.936928 999843

并且想要转换为漂亮(以便写入具有更好可读性的文本文件)

import prettytable as pt
x = pt.PrettyTable()
for col in list(df.columns):
x.add_column(col,df[col])

然后在函数内,我使用

print(x.get_string())

并收到此错误

File "<ipython-input-130-8db747160a67>", line 5, in <module>
verbose = True)

File "<ipython-input-129-4e27c067e0b5>", line 104, in lda_save_eval
print(x.get_string())

File "C:\Users\USER\Anaconda3\envs\tensorflow\lib\site-packages\prettytable.py", line 990, in get_string
self._compute_widths(formatted_rows, options)

File "C:\Users\USER\Anaconda3\envs\tensorflow\lib\site-packages\prettytable.py", line 894, in _compute_widths
widths = [_get_size(field)[0] for field in self._field_names]

File "C:\Users\USER\Anaconda3\envs\tensorflow\lib\site-packages\prettytable.py", line 894, in <listcomp>
widths = [_get_size(field)[0] for field in self._field_names]

File "C:\Users\USER\Anaconda3\envs\tensorflow\lib\site-packages\prettytable.py", line 77, in _get_size
lines = text.split("\n")

AttributeError: 'int' object has no attribute 'split'

有什么线索吗?

最佳答案

尝试使用 print (x.get_string()) 而不是 print(x.get_string)

为了改进,试试这个,为了以更好的可读性写入文本文件,您不需要循环使用 tabulate 它为您提供了更多灵 active 。

试试这个,

from tabulate import tabulate
print (tabulate(df,df.columns,tablefmt='psql'))

tablefmt中,您可以提供许多选项来获得不同的样式。有关更多详细信息,请参阅此 link

关于Python 漂亮的 get_string 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51018046/

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