gpt4 book ai didi

python - 如何从 pandas 数据框中的特定列写入多个 Excel 工作表?

转载 作者:行者123 更新时间:2023-12-01 06:22:13 25 4
gpt4 key购买 nike

这就是 df3(pandas dataframe) 的样子:

        Key        Media_type                                    cleaned_text                                     keyword            subtopic       topic
0 910040 facebook will wait outside post office post office Brand service
1 910040 facebook will wait outside post office will wait random irrelevant
2 218658 facebook there no section post office alabama ups fedex post office Brand service
3 218658 facebook there no section post office alabama ups fedex ups fedex Brand service
4 763587 twitter crazy package delivery rammed car package delivery Shipping company
5 827572 twitter formatting idead delivery done delivery done Shipping country
6 2404106 facebook supoused mexico united states america united states america location marathon
7 1077739 twitter rt ups rt ups retweet service
8 786786 extraterristrial put one toostamp (put[a-za-z]{,5}|made|leave)(.{,8})(stamp) put one's stamp on outlier
9 36283 unkown pute horse stamp (put[a-za-z]{,5}|made|leave)(.{,8})(stamp) put one's stamp on outlier

我想为 df3['subtopic'] 中的每个值放置一个过滤器,并将结果值返回到 Excel。例如。手动执行此操作的方法是:

df4 = df3.loc[df3['Subtopic'] == 'Brand']
df4.to_excel('subtopic_FedexDriver.xlsx')

这就是这段代码的输出:

Key        Media_type                                    cleaned_text                                     keyword            subtopic       topic
0 910040 facebook will wait outside post office post office Brand service
1 218658 facebook there no section post office alabama ups fedex post office Brand service
2 218658 facebook there no section post office alabama ups fedex ups fedex Brand service

我必须分别为每个子主题执行此操作。有没有办法让我自动化这个?即使这些值被添加到 1 个 Excel 文件内的不同 Excel 工作表中,也没有问题。

最佳答案

IIUC,

writer = pd.ExcelWriter('a.xlsx', engine = 'xlsxwriter')
for n, g in df.groupby('subtopic'):
g.to_excel(writer, sheet_name=n)
writer.save()
writer.close()

输出:

enter image description here

关于python - 如何从 pandas 数据框中的特定列写入多个 Excel 工作表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60309180/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com