英文:
styling a pandas table and merging 2 dataframes
问题
我想要给一个pandas表格添加样式,但是我无法使每一行交替变形,着色或设置边框。如果有人能帮助我,我会非常感激。我在下面上传了两个表格。右侧的表格是我目前的表格,左侧的表格是我的目标。
Content Based MOVIES TITLE | Content Based MOVIES ID | Colaborative Filtering MOVIES TITLE | Colaborative Filtering MOVIES ID |
---|---|---|---|
Ordinary People | 1956 | Interstate 60 | 6902 |
Firewalker | 2477 | Rubber | 81132 |
Fantastic Planet, The (Planète sauvage, La) | 2495 | Dragonheart 2: A New Beginning | 117646 |
Airport '77 | 2522 | Inside Out | 134853 |
Mister Roberts | 3035 | Wizards of Waverly Place: The Movie | 148775 |
要着色,我尝试更改表格属性,使用style.properties,并使用不同的连接和合并技巧来交替粘贴两个表格,但没有一个生效。
英文:
I wanted to style a pandas table, but I'm not able to reshape it every other row, color it this way or set borders for it. I'd be thankful if anyone helps me with this. I uploaded both tables below. The right side table is my table at the moment and the left one is my target.
Content Based MOVIES TITLE | Content Based MOVIES ID | Colaborative Filtering MOVIES TITLE | Colaborative Filtering MOVIES ID |
---|---|---|---|
Ordinary People | 1956 | Interstate 60 | 6902 |
Firewalker | 2477 | Rubber | 81132 |
Fantastic Planet, The (Planète sauvage, La) | 2495 | Dragonheart 2: A New Beginning | 117646 |
Airport '77 | 2522 | Inside Out | 134853 |
Mister Roberts | 3035 | Wizards of Waverly Place: The Movie | 148775 |
To color it, I tried to change tables properties, using style.properties and also used different join and merge techniques to stick two tables every other row, though none of'em worked.
答案1
得分: 2
你可以使用lreshape
函数来重塑你的DataFrame,然后使用apply
或applymap_index
(这些函数在Excel中友好):
out = pd.lreshape(
df, {
"TITLE": ["Content Based MOVIES TITLE", "Colaborative Filtering MOVIES TITLE"],
"ID": ["Content Based MOVIES ID", "Colaborative Filtering MOVIES ID"]
}
)
import numpy as np
# 行
purple = "background-color: #ffff99; color: black"
yellow = "background-color: #b376e0; color: black"
border = ["border-right: 3px solid black", ""]
# 表头
bgray = [
"border-right: 3px solid black; background-color: #e9e8fc; color: black",
"background-color: #e9e8fc; color: black;"
]
(
out.style
.apply(lambda x: np.where(x.index % 2, yellow, purple))
.apply(lambda _: border, axis=1)
.apply_index(lambda _: bgray, axis=1)
# .to_excel("output.xlsx", index=False) # 可选
)
输出:
英文:
You can lreshape
your DataFrame, then use apply
/applymap_index
(which are Excel friendly) :
out = pd.lreshape(
df, {
"TITLE": ["Content Based MOVIES TITLE", "Colaborative Filtering MOVIES TITLE"],
"ID": ["Content Based MOVIES ID", "Colaborative Filtering MOVIES ID"]
}
)
import numpy as np
#rows
purple = "background-color: #ffff99; color: black"
yellow = "background-color: #b376e0; color: black"
border = ["border-right: 3px solid black", ""]
#header
bgray = [
"border-right: 3px solid black; background-color: #e9e8fc; color: black",
"background-color: #e9e8fc; color: black;"
]
(
out.style
.apply(lambda x: np.where(x.index % 2, yellow, purple))
.apply(lambda _: border, axis=1)
.apply_index(lambda _: bgray, axis=1)
# .to_excel("output.xlsx", index=False) #optional
)
Output :
答案2
得分: 1
这是通过查阅文档获得的信息。
链接:https://pandas.pydata.org/docs/user_guide/style.html#Finer-Control-with-Slicing
它们展示了一个类似的示例,使用了 pd.IndexSlice
,我稍作修改。
请注意,::2
表示每隔两个项目,与 0::2
完全相同。
1::2
表示从第二个项目开始,每隔两个项目。
df = pd.DataFrame({
'columnA': [1, 3, 4, 9, 1],
'columnB': [2, 4, 5, 1, 5],
'columnC': [2, 4, 5, 10, 3]
})
idx = pd.IndexSlice
slice1 = idx[idx[::2, :]]
slice2 = idx[idx[1::2, :]]
df.style \
.set_properties(**{'background-color': '#FFFF99'}, subset=slice1) \
.set_properties(**{'background-color': '#B376E0'}, subset=slice2)
英文:
Got this by referencing the docs.
https://pandas.pydata.org/docs/user_guide/style.html#Finer-Control-with-Slicing
They show a similar example using pd.IndexSlice
, which I adapted a little.
Note that ::2
means every second item and is identical to 0::2
.
1::2
means every second item starting from the second item.
df = pd.DataFrame({
'columnA': [1, 3, 4, 9, 1],
'columnB': [2, 4, 5, 1, 5],
'columnC': [2, 4, 5, 10, 3]
})
idx = pd.IndexSlice
slice1 = idx[idx[::2, :]]
slice2 = idx[idx[1::2, :]]
df.style \
.set_properties(**{'background-color': '#FFFF99'}, subset=slice1) \
.set_properties(**{'background-color': '#B376E0'}, subset=slice2)
答案3
得分: 0
Pandas的样式在正常情况下运行得很好,但对于任何不明显的情况,配置起来可能会很困难。如果您的项目只是一次性的,不需要自动化,那么更简单的解决方案是在Python环境之外的任何文档/电子表格软件中进行格式化。或者,如果需要自动化,您可以使用HTML进行格式化(从DataFrame中提取数据并放入进行HTML格式化的字符串);然后可以对HTML进行任何您想要的操作,例如导出或在您的环境中显示。
要将右侧子表格放在左侧子表格下方,您可以使用pd.concat函数。
英文:
Pandas' styling works well when it works, but for anything non-obvious it is difficult to configure it. If your project is one time thing and you don't need automation an easer solution would be to format it outside of Python environement in any document/spread-sheet software. Or if being automated is a requirement you could do formatting with HTML (extract data from the DataFrame and put it into a string that does HTML formatting); and do whatever you want with the HTML -- export it or display it inside your environement.
To put your right subtable under the left one you could use pd.concat function.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论