- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
pandas.io.formats.style.Styler.format
的公共(public)文档说
subset : IndexSlice
An argument toDataFrame.loc
that restricts which elementsformatter
is applied to.
_non_reducing_slice
东西?
if subset is None:
row_locs = range(len(self.data))
col_locs = range(len(self.data.columns))
else:
subset = _non_reducing_slice(subset)
if len(subset) == 1:
subset = subset, self.data.columns
sub_df = self.data.loc[subset]
.loc[]
兼容的东西时,我得到了一个错误。 :
>>> import pandas as pd
>>>
>>> df = pd.DataFrame([dict(a=1,b=2,c=3),dict(a=3,b=5,c=4)])
>>> df = df.set_index('a')
>>> print df
b c
a
1 2 3
3 5 4
>>> def J(x):
... return '!!!%s!!!' % x
...
>>> df.style.format(J, subset=[3])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "c:\app\python\anaconda\2\lib\site-packages\pandas\io\formats\style.py", line 372, in format
sub_df = self.data.loc[subset]
File "c:\app\python\anaconda\2\lib\site-packages\pandas\core\indexing.py", line 1325, in __getitem__
return self._getitem_tuple(key)
File "c:\app\python\anaconda\2\lib\site-packages\pandas\core\indexing.py", line 841, in _getitem_tuple
self._has_valid_tuple(tup)
File "c:\app\python\anaconda\2\lib\site-packages\pandas\core\indexing.py", line 189, in _has_valid_tuple
if not self._has_valid_type(k, i):
File "c:\app\python\anaconda\2\lib\site-packages\pandas\core\indexing.py", line 1418, in _has_valid_type
(key, self.obj._get_axis_name(axis)))
KeyError: 'None of [[3]] are in the [columns]'
>>> df.loc[3]
b 5
c 4
Name: 3, dtype: int64
>>> df.loc[[3]]
b c
a
3 5 4
IndexSlice
而且看起来很不稳定——在某些情况下有效,在其他情况下无效,至少在 Pandas 0.20.3 中:
Python 2.7.14 |Anaconda custom (64-bit)| (default, Oct 15 2017, 03:34:40) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas as pd
>>> import numpy as np
>>> idx = pd.IndexSlice
>>> r = np.arange(16).astype(int)
>>> colors = 'red green blue yellow'.split()
>>> df = pd.DataFrame(dict(a=[colors[i] for i in r//4], b=r%4, c=r*100)).set_index(['a','b'])
>>> print df
c
a b
red 0 0
1 100
2 200
3 300
green 0 400
1 500
2 600
3 700
blue 0 800
1 900
2 1000
3 1100
yellow 0 1200
1 1300
2 1400
3 1500
>>> df.loc[idx['yellow']]
c
b
0 1200
1 1300
2 1400
3 1500
>>> def J(x):
... return '!!!%s!!!' % x
...
>>> df.style.format(J,idx['yellow'])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "c:\app\python\anaconda\2\lib\site-packages\pandas\io\formats\style.py", line 372, in format
sub_df = self.data.loc[subset]
File "c:\app\python\anaconda\2\lib\site-packages\pandas\core\indexing.py", line 1325, in __getitem__
return self._getitem_tuple(key)
File "c:\app\python\anaconda\2\lib\site-packages\pandas\core\indexing.py", line 836, in _getitem_tuple
return self._getitem_lowerdim(tup)
File "c:\app\python\anaconda\2\lib\site-packages\pandas\core\indexing.py", line 948, in _getitem_lowerdim
return self._getitem_nested_tuple(tup)
File "c:\app\python\anaconda\2\lib\site-packages\pandas\core\indexing.py", line 1023, in _getitem_nested_tuple
obj = getattr(obj, self.name)._getitem_axis(key, axis=axis)
File "c:\app\python\anaconda\2\lib\site-packages\pandas\core\indexing.py", line 1541, in _getitem_axis
return self._getitem_iterable(key, axis=axis)
File "c:\app\python\anaconda\2\lib\site-packages\pandas\core\indexing.py", line 1081, in _getitem_iterable
self._has_valid_type(key, axis)
File "c:\app\python\anaconda\2\lib\site-packages\pandas\core\indexing.py", line 1418, in _has_valid_type
(key, self.obj._get_axis_name(axis)))
KeyError: "None of [['yellow']] are in the [columns]"
>>> pd.__version__
u'0.20.3'
>>> df.style.format(J,idx['yellow'])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "c:\app\python\anaconda\2\lib\site-packages\pandas\io\formats\style.py", line 401, in format
sub_df = self.data.loc[subset]
File "c:\app\python\anaconda\2\lib\site-packages\pandas\core\indexing.py", line 1494, in __getitem__
return self._getitem_tuple(key)
File "c:\app\python\anaconda\2\lib\site-packages\pandas\core\indexing.py", line 868, in _getitem_tuple
return self._getitem_lowerdim(tup)
File "c:\app\python\anaconda\2\lib\site-packages\pandas\core\indexing.py", line 969, in _getitem_lowerdim
return self._getitem_nested_tuple(tup)
File "c:\app\python\anaconda\2\lib\site-packages\pandas\core\indexing.py", line 1048, in _getitem_nested_tuple
obj = getattr(obj, self.name)._getitem_axis(key, axis=axis)
File "c:\app\python\anaconda\2\lib\site-packages\pandas\core\indexing.py", line 1902, in _getitem_axis
return self._getitem_iterable(key, axis=axis)
File "c:\app\python\anaconda\2\lib\site-packages\pandas\core\indexing.py", line 1205, in _getitem_iterable
raise_missing=False)
File "c:\app\python\anaconda\2\lib\site-packages\pandas\core\indexing.py", line 1161, in _get_listlike_indexer
raise_missing=raise_missing)
File "c:\app\python\anaconda\2\lib\site-packages\pandas\core\indexing.py", line 1246, in _validate_read_indexer
key=key, axis=self.obj._get_axis_name(axis)))
KeyError: u"None of [Index([u'yellow'], dtype='object')] are in the [columns]"
>>> pd.__version__
u'0.24.2'
df.style.format(J,idx['yellow',:])
最佳答案
我同意您表现出的行为并不理想。
>>> df = (pandas.DataFrame([dict(a=1,b=2,c=3),
dict(a=3,b=5,c=4)])
.set_index('a'))
>>> df.loc[[3]]
b c
a
3 5 4
>>> df.style.format('{:.2f}', subset=[3])
Traceback (most recent call last)
...
KeyError: "None of [Int64Index([3], dtype='int64')] are in the [columns]"
pandas.IndexSlice
来解决此问题。作为子集参数:
>>> df.style.format('{:.2f}', subset=pandas.IndexSlice[[3], :])
_non_reducing_slice()
正在做,它的目标是合理的(确保一个子集不会将维度降低到系列)。它的实现将列表视为一系列列名:
From pandas/core/indexing.py:
def _non_reducing_slice(slice_):
"""
Ensurse that a slice doesn't reduce to a Series or Scalar.
Any user-paseed `subset` should have this called on it
to make sure we're always working with DataFrames.
"""
# default to column slice, like DataFrame
# ['A', 'B'] -> IndexSlices[:, ['A', 'B']]
kinds = (ABCSeries, np.ndarray, Index, list, str)
if isinstance(slice_, kinds):
slice_ = IndexSlice[:, slice_]
...
subset=[3]
引发的异常匹配
df[[3]]
的行为而不是
df.loc[[3]]
.
关于python - pandas.io.formats.style.Styler.format 中的子集参数有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59203022/
这两个文件之间的主要区别是什么:styles.xml (res\values\styles.xml) 和 styles.xml (res\values-v21\styles.xml ? 针对旧的and
我正在尝试将按钮样式设置为看起来像我在 Android Full Width ICS style Minimalist Bottom ButtonsViews 中询问的那些按钮. 我已经成功了,有兴趣
只是想检查一下: 如果我有 Style.css 和 Style.min.css(在同一目录中)并且我的 html 页面引用了 Style.css,浏览器/服务器是否会下载 Style.min.css?
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
从上面的问题,我认为这会相对容易,但我找不到任何关于如何向“样式”下拉菜单添加样式的文档。谁能把我推向正确的方向? 最佳答案 样式下拉列表会根据主题的typography.css 文件中的类自动填充。
我有两种风格 还有这个 如果我尝试在这样的对象上使用第二个 编译器抛出这个错误: 错误 16 Style 对象不能影响它所应用的对象的 St
我想知道是否有关于在 Lisp 中使用标签的标准做法。我一直在弄乱这里第一个答案中描述的算法的 Lisp 实现 Generating permutations lazily我当前的版本使用标签来分解部
我想以编程方式获取样式为“ButtonBar”的 LinearLayout 的背景颜色。 我试过用LinearLayout的getBackgroundColor,没找到方法。 有人有想法吗?问候 最
我在扩展 javax.swing.text.DefaultStyledDocument 的类中遇到间歇性问题。该文档正在发送到打印机。大多数情况下,文档的格式看起来是正确的,但有时却并非如此。看起来格
我想将所有元素设为边框。我想这样做: * { box-sizing: border-box; } 如何使用 React 的内联样式做到这一点?我不想在每个组件中都写这个规则... 最佳答案 这是不
当我创建一个 Android 应用程序项目时,我在 (android:theme="@style/AppTheme") 上的 AndroidManifest.xml 中出现错误 找不到与给定名称匹配的
一种风格ol.layer.Vector可以设置为 ol.style.Style ,样式函数或 ol.style.Style 的数组.数组的用途和作用——与仅传递 ol.style.Style 相比目的
我的意思是内部风格 #div {color:red;} document.getElementsByTagName('style').innerHTML 不工作... document.style
http://synergine.net/rain.php 你好。我试图清除 .ripple div 中所有元素的样式属性,但没有成功: function contact(level){ focus_
我使用 vue 和 v-for 循环来创建跨度。以下是使用 bootstrap4 的背景颜色的一种样式的成功: {{ group }} export default {
有没有办法只存储元素的当前样式状态,这样我就可以搞砸样式然后再重置它? 类似于(虽然这不起作用):http://jsfiddle.net/843Pj/ var el=document.getEleme
我正在尝试将 tinymce 配置为不允许在 style 属性中使用 css 样式。 我只想允许一种样式,即文本装饰。这是一个类似的问题 http://tinymce.moxiecode.com/pu
我对style.css做了一些修改,上传到网上。但是它没有显示我需要的结果。即它仍然采用旧的 style.css 代码。 我可以离线查看更改,但是当我给它完整的 href 链接时,它没有显示必要的结果
我添加到 web 文件夹下的样式文件夹似乎没有被我的 JSP 上的调度程序 servlet 映射。我明白了 WARN : org.springframework.web.servlet.PageNot
是否有任何用于 JQuery 数据表的 Metro Style CSS 样式插件? 最佳答案 看看here 或者您可以自己创建一个。 Metro 风格很容易用 Segoe 字体复制 关于jquery
我是一名优秀的程序员,十分优秀!