假设我们有一个数据帧,我想给不同的单元格上色:
['Arizona','company'](1st)
,['Texas','size'](1099)
为绿色。在['Florida','veterans'](26)
,['Maine','armored'](0)
呈红色。在有什么好办法?
raw_data = {'regiment': ['Nighthawks', 'Nighthawks', 'Nighthawks', 'Nighthawks', 'Dragoons', 'Dragoons', 'Dragoons', 'Dragoons', 'Scouts', 'Scouts', 'Scouts', 'Scouts'],
'company': ['1st', '1st', '2nd', '2nd', '1st', '1st', '2nd', '2nd','1st', '1st', '2nd', '2nd'],
'deaths': [523, 52, 25, 616, 43, 234, 523, 62, 62, 73, 37, 35],
'battles': [5, 42, 2, 2, 4, 7, 8, 3, 4, 7, 8, 9],
'size': [1045, 957, 1099, 1400, 1592, 1006, 987, 849, 973, 1005, 1099, 1523],
'veterans': [1, 5, 62, 26, 73, 37, 949, 48, 48, 435, 63, 345],
'readiness': [1, 2, 3, 3, 2, 1, 2, 3, 2, 1, 2, 3],
'armored': [1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1],
'deserters': [4, 24, 31, 2, 3, 4, 24, 31, 2, 3, 2, 3],
'origin': ['Arizona', 'California', 'Texas', 'Florida', 'Maine', 'Iowa', 'Alaska', 'Washington', 'Oregon', 'Wyoming', 'Louisana', 'Georgia']}
df = pd.DataFrame(raw_data, columns = ['regiment', 'company', 'deaths', 'battles', 'size', 'veterans', 'readiness', 'armored', 'deserters', 'origin'])
df = df.set_index('origin')
df.head()
(http://chrisalbon.com/python/pandas_indexing_selecting.html)
您可以将slicing in Style与参数
subset
和函数Styler.applymap
一起使用对于元素样式,请在jupyter notebook
中运行代码:如果只需要更改}一起用于表格样式,并且函数必须返回一个具有相同索引和列标签的
^{pr2}$DataFrame
中的某些值,则可以将Styler.apply
与{DataFrame
:试试这个来自http://melissagymrek.com/python/2014/01/12/ipython-tables.html的例子
http://melissagymrek.com/python/2014/01/12/ipython-tables.html
相关问题 更多 >
编程相关推荐