Is it possible to display pandas styles in the IPython console?

2024/11/17 16:23:00

Is it possible to display pandas styles in an iPython console? The following code in a Jupyter notebook

import pandas as pd
import numpy as npnp.random.seed(24)
df = pd.DataFrame({'A': np.linspace(1, 10, 5)})
df = pd.concat([df, pd.DataFrame(np.random.randn(5, 1), columns=list('B'))],axis=1)
df.style.format({'B': "{:.2%}"})

correctly produces

enter image description here

In the console I only get

In [294]: df.style.format({'B': "{:.2%}"})
Out[294]: <pandas.io.formats.style.Styler at 0x22f3f4fe780>

Is it possible to achieve a similar result here, or is the style engine dependent on an html frontend?

Thanks in advance for any help.

Answer

I believe that the styler really requires an html frontend, like jupyter, even if it only formats numbers (and not fonts or colors).

See e.g. here Using Pandas Styler.

In order to convert a column to a specific format, one should use the .map method:

df = pd.DataFrame({'A': np.linspace(1, 5, 5)})
df = pd.concat([df, pd.DataFrame(np.random.randn(5, 1), columns=list('B'))],axis=1)df['C']=df['B'].map("{:.2%}".format)print(df, end='\n\n')
print(df.dtypes)A         B         C
0  1.0  1.329212   132.92%
1  2.0 -0.770033   -77.00%
2  3.0 -0.316280   -31.63%
3  4.0 -0.990810   -99.08%
4  5.0 -1.070816  -107.08%A    float64
B    float64
C     object
dtype: object

The drawback is, that df['C'] is not a number anymore, so you cannot properly sort the dataframe anymore.

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

Related Q&A

HEAD method not allowed after upgrading to django-rest-framework 3.5.3

We are upgrading django-rest-framework from 3.1.3 to 3.5.3. After the upgrade all of our ModelViewSet and viewsets.GenericViewSet views that utilize DefaultRouter to generate the urls no longer allow …

How do I specify server options?

Im trying to run gRPC server in Python. I found a way to do it like this:import grpc from concurrent import futuresserver = grpc.server(futures.ThreadPoolExecutor(max_workers=100)) ... # add my grpc se…

How to find collocations in text, python

How do you find collocations in text? A collocation is a sequence of words that occurs together unusually often. python has built-in func bigrams that returns word pairs. >>> bigrams([more, i…

How to set size of a Gtk Image in Python

How can I set the width and height of a GTK Image in Python 3.

Numpy Vectorized Function Over Successive 2d Slices

I have a 3D numpy array. I would like to form a new 3d array by executing a function on successive 2d slices along an axis, and stacking the resulting slices together. Clearly there are many ways to do…

MySQL and Python Select Statement Issues

Thanks for taking the time to read this. Its going to be a long post to explain the problem. I havent been able to find an answer in all the usual sources.Problem: I am having an issue with using the …

How to pass variable in url to Django List View

I have a Django generic List View that I want to filter based on the value entered into the URL. For example, when someone enters mysite.com/defaults/41 I want the view to filter all of the values mat…

Django Select Option selected issue

I tried to follow some examples on stackoverflow for option selected in select list but still, I could not get it work.This is my code snippet<select name="topic_id" style="width:90%&…

reading tab-delimited data without header in pandas

Im having trouble using pandas to open tab-delimited data without headers.My test data (actually contains 200 lines, of which I am showing the first 10):Tag19184 CTAAC hffef 1 a 36 - chr1…

Python Try/Except with multiple except blocks

try:raise KeyError() except KeyError:print "Caught KeyError"raise Exception() except Exception:print "Caught Exception"As expected, raising Exception() on the 5th line isnt caught i…