Pandas version checks
-
[X] I have checked that this issue has not already been reported.
-
[X] I have confirmed this bug exists on the latest version of pandas.
-
[ ] I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
import pandas as pd
df = pd.DataFrame([2,3])
df.index.name = 'a'
print(df.to_string(header=False))
Issue Description
The code results in
a 2
0 3
1
Expected Behavior
a
0 2
1 3
Installed Versions
Comment From: jondo
Workaround: reset df.index.name
to None
before calling df.to_string
.
Comment From: vamsi-verma-s
use index_names=False
print(df.to_string(header=False, index_names=False))
0 2
1 3
Comment From: jondo
@vamsi-verma-s, thank you for pointing out the index_names
argument.
In my case df.to_string(header=False)
evaluates to the string 'a 2\n0 3\n1 '
, which nonsensically has the index name and the first value of the data column in the same row, and the rest of the index shifted w.r.t the data.
I now updated my report: I expect df.to_string(header=False)
to give 'a \n0 2\n1 3'
, analogous to the ' 0\na \n0 2\n1 3' of
df.to_string()`.
Comment From: andrewchen1216
take
Comment From: attack68
Note that Styler.to_string
is also a workaround
>>> print(df.style.hide(axis="columns").to_string())
a
0 2
1 3
>>> print(df.style.hide(axis="columns").hide(axis="index", names=True).to_string())
0 2
1 3