pandas :如何获取.to_string()方法以将列标题与列值对齐? [英] pandas: How to get .to_string() method to align column headers with column values?
问题描述
这已经困扰了我一段时间,我觉得必须要有解决方案,因为打印数据帧总是使列标题与它们各自的值对齐.
This has been stumping me for a while and I feel like there has to be a solution since printing a dataframe always aligns the columns headers with their respective values.
示例:
df = pd.DataFrame({'First column name': [1234, 2345, 3456], 'Second column name': [5432,4321,6543], 'Third column name': [1236,3457,3568]})
df_string = df.to_string(justify='left', col_space='30')
现在,当您打印df_string时,您将获得所需的格式:
now when you print df_string, you get the desired formatting:
但是当我获取字符串并查看它时(在这种情况下,我将字符串传递给显示文本的PyQt小部件),这是输出:
but when I take the string and view it (in this case, I'm passing the string to a PyQt widget that displays text), this is the output:
(字符串在我的控制台上的显示方式):
(this is how the string appears on my console):
任何帮助是极大的赞赏.
Any help is greatly appreciated.
推荐答案
这很好地对齐了列标题:
This lines up column headers nicely:
print(df.to_string())
但是这也会打印索引.如果您不想打印索引,可以:
But this prints indices too. If you don't want to print the indices, you can:
print(df.to_string(index=False)
问题是,列标题不再正确对齐.
Problem is, the column headers no longer line up correctly.
所以我写了这个hack:
So I wrote this hack:
blanks = r'^ *([a-zA-Z_0-9-]*) .*$'
blanks_comp = re.compile(blanks)
def find_index_in_line(line):
index = 0
spaces = False
for ch in line:
if ch == ' ':
spaces = True
elif spaces:
break
index += 1
return index
def pretty_to_string(df):
lines = df.to_string().split('\n')
header = lines[0]
m = blanks_comp.match(header)
indices = []
if m:
st_index = m.start(1)
indices.append(st_index)
non_header_lines = lines[1:len(lines)]
for line in non_header_lines:
index = find_index_in_line(line)
indices.append(index)
mn = np.min(indices)
newlines = []
for l in lines:
newlines.append(l[mn:len(l)])
return '\n'.join(newlines)
您这样调用:
print(pretty_to_string(df))
该代码通过调用df.to_string()(其中各列排列整齐)来工作,并计算索引列占用的最大字符数.
The code works by calling df.to_string() (where columns are lined up nicely) and calculates the max # of characters taken up by the index column.
然后从每行剥离索引.
It then strips off the indices from each line.
这篇关于 pandas :如何获取.to_string()方法以将列标题与列值对齐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!