pandas: How to get .to_string() method to align column headers with column values?

2024/10/14 1:10:55

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:http://i.imgur.com/Xyoy4Op.png

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: http://i.imgur.com/a1NcBQA.png

(this is how the string appears on my console): http://i.imgur.com/WRHEhKB.png



Any help is greatly appreciated.

Answer

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'

https://en.xdnf.cn/q/69472.html

Related Q&A

Do I need to use `nogil` in Cython

I have some Cython code that Id like to run as quickly as possible. Do I need to release the GIL in order to do this? Lets suppose my code is similar to this: import numpy as np# trivial definition ju…

supervisord environment variables setting up application

Im running an application from supervisord and I have to set up an environment for it. There are about 30 environment variables that need to be set. Ive tried putting all on one bigenvironment=line and…

Updating gui items withing the process

I am trying to make a GUI for my app and ran into a problem: using PySimpleGUI I have to define layout at first and only then display the whole window. Right now the code is like this:import PySimpleGU…

UnicodeDecodeError with Djangos request.FILES

I have the following code in the view call..def view(request):body = u"" for filename, f in request.FILES.items():body = body + Filename: + filename + \n + f.read() + \nOn some cases I getU…

bifurcation diagram with python

Im a beginner and I dont speak english very well so sorry about that. Id like to draw the bifurcation diagram of the sequence : x(n+1)=ux(n)(1-x(n)) with x(0)=0.7 and u between 0.7 and 4.I am supposed …

How do I use nordvpn servers as python requests proxies

Dont ask how, but I parsed the server endpoints of over 5000 nordvpn servers. They usually are something like ar15.nordvpn.com for example. Im trying to use nordvpn servers as request proxies. I know i…

Python Proxy Settings

I was using the wikipedia module in which you can get the information that is present about that topic on wikipedia. When I run the code it is unable to connect because of proxy. When I connected PC to…

Docker - Run Container from Inside Container

I have two applications:a Python console script that does a short(ish) task and exits a Flask "frontend" for starting the console app by passing it command line argumentsCurrently, the Flask …

Way to run Maven from Python script?

(I am using Windows.)I am trying to run maven from a python script. I have this:import subprocessmvn="C:\\_home\\apache-maven-2.2.1\\bin\\mvn.bat --version" p = subprocess.Popen(mvn, shell=Tr…

Why does testing `NaN == NaN` not work for dropping from a pandas dataFrame?

Please explain how NaNs are treated in pandas because the following logic seems "broken" to me, I tried various ways (shown below) to drop the empty values.My dataframe, which I load from a C…