英文:
How do you transform a Pandas data frame into category and list of items?
问题
我正在尝试将任务列表输出为PDF。输出应为:
- 廊道
- 任务
- 任务
- 任务
- 前厅
- 任务
- 任务
- 任务
- [房间名]
- 任务
- ...
我有以下代表需要在给定周内在每个房间中发生的清洁任务的Pandas数据框。
Room Task Weeks
0 廊道 吸尘地板 ABCD
2 廊道 拖地板 ABCD
4 前厅 吸尘地板 ABCD
5 前厅 清空吸尘器 ABCD
6 前厅 用微湿布擦拭踢脚板 A
7 前厅 拖地板 ABCD
8 前厅 如果桌布脏了,换桌布 ABCD
10 客厅 吸尘地板 ABCD
11 客厅 用微湿布擦拭踢脚板 A
12 客厅 整理沙发 ABCD
我尝试了groupby
:
today_tasks = (
task_data[task_data["Weeks"].str.contains(schedule_week)]
.groupby("Room", group_keys=True)
.apply(lambda x: x)
)
print(today_tasks.head(10))
它返回以下结果:
Room Task Weeks
Room
浴室 62 浴室 吸尘地板 ABCD
63 浴室 拖地板 ABCD
64 浴室 清洁浴缸 ABCD
65 浴室 清洁淋浴喷头 AC
66 浴室 清洁淋浴玻璃 AC
68 浴室 清洁马桶 ABCD
69 浴室 清洁水槽 AC
楼上前卧室 45 楼上前卧室 吸尘地板 ABCD
46 楼上前卧室 拖地板 ABCD
47 楼上前卧室 从床上取下床单 ABCD
我明白我可以通过循环将数据框放入字典中,但这并不感觉很符合Pandas的风格。然而,这个集合只有70行,而且每周只需要执行一次,所以性能可能并不那么重要。尽管如此,我还是更喜欢遵守最佳实践。是否有更好的方法?
英文:
I am trying to output a task list as PDF. The output should be
- Hallway
- Task
- Task
- Task
- Front Room
- Task
- Task
- Task
- [Room name]
- Task
- ...
I have the following Pandas data frame which represents the cleaning tasks that need to occur in each room on a given week.
Room Task Weeks
0 Hallway Vacuum floor ABCD
2 Hallway Mop floor ABCD
4 Front Room Vacuum floor ABCD
5 Front Room Empty Vacuum Cleaner ABCD
6 Front Room Wipe skirting boards with slightly damp cloth A
7 Front Room Mop floor ABCD
8 Front Room If tablecloth is dirty, change tablecloth ABCD
10 Living Room Vacuum floor ABCD
11 Living Room Wipe skirting boards with slightly damp cloth A
12 Living Room Tidy sofa ABCD
I have tried groupby
today_tasks = (
task_data[task_data["Weeks"].str.contains(schedule_week)]
.groupby("Room", group_keys=True)
.apply(lambda x: x)
)
print(today_tasks.head(10))
Which returns the following
Room Task Weeks
Room
Bathroom 62 Bathroom Vacuum floor ABCD
63 Bathroom Mop floor ABCD
64 Bathroom Clean bath ABCD
65 Bathroom Clean shower heads AC
66 Bathroom Clean shower glass AC
68 Bathroom Clean toilet ABCD
69 Bathroom Clean sink AC
Front Bedroom Upstairs 45 Front Bedroom Upstairs Vacuum floor ABCD
46 Front Bedroom Upstairs Mop floor ABCD
47 Front Bedroom Upstairs Remove sheets from bed ABCD
I appreciate that I could for
through and put the data frame into a dictionary, but this does not feel very Pandas-native. Equally, however, the set is only 70 rows long and only needs to be executed weekly, so performance probably does not matter too much. Nonetheless, I do prefer to abide by best practices.
Is there a better way to do it?
答案1
得分: 1
你可以使用 `jinja2` 渲染 HTML 模板,然后通过外部工具如 `wkhtmltopdf` 和 `pdfkit` 库将其转换为 PDF:
```python
import jinja2
import io
templateLoader = jinja2.FileSystemLoader(searchpath="./")
templateEnv = jinja2.Environment(loader=templateLoader)
def render_pdf(filename, **kwargs):
template = templateEnv.get_template('template.html')
output = template.render(**kwargs)
return output
for week, subdf in df.groupby('Weeks'):
room_tasks = subdf.groupby('Room')['Task'].agg(list).to_dict()
print(render_html(week=week, room_tasks=room_tasks))
输出:
<!-- 周 A -->
<html>
<title>周 A</title>
<body>
<ul>
<li>前厅</li>
<ul>
<li>用微湿布擦踢脚板</li>
</ul>
</ul>
<ul>
<li>客厅</li>
<ul>
<li>用微湿布擦踢脚板</li>
</ul>
</ul>
</body>
</html>
<!-- 周 ABCD -->
<html>
<title>周 ABCD</title>
<body>
<ul>
<li>前厅</li>
<ul>
<li>吸尘地板</li>
</ul><ul>
<li>清空吸尘器</li>
</ul><ul>
<li>拖地</li>
</ul><ul>
<li>如果桌布脏了,换桌布</li>
</ul>
</ul>
<ul>
<li>走廊</li>
<ul>
<li>吸尘地板</li>
</ul><ul>
<li>拖地</li>
</ul>
</ul>
<ul>
<li>客厅</li>
<ul>
<li>吸尘地板</li>
</ul><ul>
<li>整理沙发</li>
</ul>
</ul>
</body>
</html>
template.html
:
<html>
<title>周 {{ week }}</title>
<body>
{%- for room, tasks in room_tasks.items() %}
<ul>
<li>{{ room }}</li>
{% for task in tasks -%}
<ul>
<li>{{ task }}</li>
</ul>
{%- endfor %}
</ul>
{%- endfor %}
</body>
</html>
英文:
You can render HTML template with jinja2
then convert to PDF with external tool like wkhtmltopdf
via pdfkit
library:
import jinja2
import io
templateLoader = jinja2.FileSystemLoader(searchpath="./")
templateEnv = jinja2.Environment(loader=templateLoader)
def render_pdf(filename, **kwargs):
template = templateEnv.get_template('template.html')
output = template.render(**kwargs)
return output
for week, subdf in df.groupby('Weeks'):
room_tasks = subdf.groupby('Room')['Task'].agg(list).to_dict()
print(render_html(week=week, room_tasks=room_tasks))
Output:
<!-- Week A -->
<html>
<title>Week A</title>
<body>
<ul>
<li>Front Room</li>
<ul>
<li>Wipe skirting boards with slightly damp cloth</li>
</ul>
</ul>
<ul>
<li>Living Room</li>
<ul>
<li>Wipe skirting boards with slightly damp cloth</li>
</ul>
</ul>
</body>
</html>
<!-- Week ABCD -->
<html>
<title>Week ABCD</title>
<body>
<ul>
<li>Front Room</li>
<ul>
<li>Vacuum floor</li>
</ul><ul>
<li>Empty Vacuum Cleaner</li>
</ul><ul>
<li>Mop floor</li>
</ul><ul>
<li>If tablecloth is dirty, change tablecloth</li>
</ul>
</ul>
<ul>
<li>Hallway</li>
<ul>
<li>Vacuum floor</li>
</ul><ul>
<li>Mop floor</li>
</ul>
</ul>
<ul>
<li>Living Room</li>
<ul>
<li>Vacuum floor</li>
</ul><ul>
<li>Tidy sofa</li>
</ul>
</ul>
</body>
</html>
template.html
<html>
<title>Week {{ week }}</title>
<body>
{%- for room, tasks in room_tasks.items() %}
<ul>
<li>{{ room }}</li>
{% for task in tasks -%}
<ul>
<li>{{ task }}</li>
</ul>
{%- endfor %}
</ul>
{%- endfor %}
</body>
</html>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论