Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas print Series elements as "repr" not "str"

Tags:

python

pandas

When you print a Series with with dtype object or string, the elements are printed as if you called str().

For example, print(pd.Series(['a', 'b', 'c'])) yields:

0    a
1    b
2    c
dtype: object

Is it possible to tell Pandas to use repr() and not str() to render elements for printing? The desired output in the example above would be:

0    'a'
1    'b'
2    'c'
dtype: object

As a workaround, you can call my_series.apply(repr), but it would be nice if I could set this as some kind of Pandas option more generally. I looked in the list of Pandas options in the docs and didn't see anything like this.

like image 814
shadowtalker Avatar asked Oct 30 '25 18:10

shadowtalker


1 Answers

This does not currently seem possible in Pandas. I have made a feature request here: https://github.com/pandas-dev/pandas/issues/46744 but this might be considered too niche to be worth adding to Pandas.

The best workaround is to use DataFrame.to_string, with repr as the formatter for every string/object column.

like image 164
shadowtalker Avatar answered Nov 01 '25 07:11

shadowtalker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!