本文是小编为大家收集整理的关于pandas:如何让.to_string()方法将列标题与列值对齐?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。
问题描述
这已经使我陷入困境了一段时间,我觉得必须有一个解决方案,因为打印dataFrame总是将列标题与各自的值保持一致.
示例:
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')
但是,当我拿起字符串并查看它时(在这种情况下,我将字符串传递到显示文本的PYQT小部件)时,这是输出:
任何帮助是极大的赞赏.
推荐答案
此列排列很好:
print(df.to_string())
但这也打印了索引.如果您不想打印索引,则可以:
print(df.to_string(index=False)
问题是,列标题不再正确排队.
所以我写了这个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()(在其中排列得很好)并计算索引列占用的字符的最大值.
.然后将每行的索引剥离.
其他推荐答案
要解决此问题,您可以在to_string args中设置header = false,然后设置带有列名称的字符串,并带有右间距以匹配表的列. 在表格之前打印您的列字符串,并且应该对其进行排列.这可能需要一些调整,但尽管有些刺耳,但它起作用了.
示例='Col 1 Col2 Col3 Col4'
问题描述
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.
example:
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')
now when you print df_string, you get the desired formatting:
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.
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)
Which you invoke like this:
print(pretty_to_string(df))
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.
其他推荐答案
To get around this issue, you can set header=False in the to_string args, then set up a string with the column names with the right spacing to match the columns of the table. Print your Columns string before the table and it should all line up. It may take a little tweaking, but it works out albeit a bit hacky.
Example = 'col 1 col2 col3 col4'