繁体   English   中英

如何从熊猫数据框中的特定列编写多个 Excel 表?

[英]How to write multiple excel sheets from a particular column in a pandas dataframe?

这就是 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 表中,也没关系。

国际大学联盟,

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()

输出:

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM