English 中文(简体)
使用百分数的仪表组群的颜色背景
原标题:Colouring background of dataframe cells using percentiles

因此,我有一栏和一行的分光仪:

df = pd.read_csv( percentiles.csv , index_col=0, parse_dates=True)

最近的3个浏览点类似:

ATH ATL 12MH 12ML 3MH 3ML 1MH 1ML
Date
2024-02-16 6 0 8 -1 11 -8 15 -16
2024-02-19 8 -1 10 -2 11 -5 22 -11
2024-02-20 8 0 13 0 16 -2 40 -3

试图作为表格(pdf)出口该图片,其背景取决于该数值高或低。 发现的一种方法是使用 percent。

为了确定每一栏的百分点,我已经确定了另一个数据框架:

percentiles = [0, 0.1, 0.2, 0.8, 0.9]
df2 = df.quantile(q=percentiles, axis=0)

df2包含每个栏的百分数值,即:

ATH ATL 12MH 12ML 3MH 3ML 1MH 1ML
0.0 0.0 -115.0 0.0 -74.0 0.0 -122.0 0.0 -136.0
0.1 0.0 -8.0 0.0 -8.0 1.0 -26.1 4.0 -44.1
0.2 1.8 -4.0 1.0 -4.0 3.0 -14.0 7.0 -28.0
0.8 10.0 0.0 11.0 0.0 20.0 -1.0 33.0 -4.0
0.9 15.0 0.0 16.0 0.0 29.0 0.0 44.1 -2.0

冰雪是个字典,每个百分点都有一个颜色:

percentile_color = {0: red , 0.1:  orange , 0.2:  white , 0.8:  lightgreen , 0.9:  green }

每一栏中的每个电池按其百分位的颜色标示。 我可以做一系列工作(栏目),但一旦我提出数据框架,每栏的百分点值各异,一 st。 任何建议? 感谢!

问题回答

您可使用er.apply 。 https://pandas.pydata.org

def get_colors(s):
    s = s.astype(float).sort_values()
    return (pd.merge_asof(s.reset_index(), s.quantile(q=percentiles).reset_index())
              .set_index( Date )[ index ]
              .map(lambda x: f background-color: {percentile_color.get(x, "")} )
           )

df.style.apply(get_colors)

产出:

“color

请注意,如果一个电池的颜色与<代码>direction相吻合,你可以选择:

def get_colors(s):
    s = s.astype(float).sort_values()
    return (pd.merge_asof(s.reset_index(), s.quantile(q=percentiles).reset_index(),
                         direction= forward )
              .set_index( Date )[ index ]
              .map(lambda x: f background-color: {percentile_color.get(x, "")} )
           )

df.style.apply(get_colors)

产出:

“color

而且,对于重复的qu,你可以把最小的les带上:

def get_colors(s):
    s = s.astype(float).sort_values()
    return (pd.merge_asof(s.reset_index(),
                          s.quantile(q=percentiles).drop_duplicates().reset_index())
              .set_index( Date )[ index ]
              .map(lambda x: f background-color: {percentile_color.get(x, "")} )
           )

df.style.apply(get_colors)

产出:

“color





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签