简体   繁体   English

将 Pandas 数据框写入多个 excel 文件

[英]Writing pandas dataframes to multiple excel files

Image shows a pandas dataframe图像显示了一个熊猫数据框

import pandas as pd
import numpy as np

file = '/Dummy.xlsx'

Customer = pd.read_excel(file, sheet_name=0)
Items = pd.read_excel(file, sheet_name=1)
Commission = pd.read_excel(file, sheet_name=2)
Price = pd.read_excel(file, sheet_name=3)
Sheet1 = pd.read_excel(file, sheet_name=4) 

Inner_join = pd.merge(Price,Sheet1,on = 'Item_ID', how='inner')

Inner_join = pd.merge(Price, Sheet1,on = 'Item_ID', how='inner').merge(Commission, on = 'Commission_ID')

joined_table = pd.merge(Inner_join, Customer, right_on = 'Customer_ID', left_on = 'Customer_ID_x', how = 'inner')

final_table = joined_table[['Date','Customer_Name', 'Customer_ID','Item_Name', 'Commission', 'Qty','Base_Price','Rate']]

calculated_commission = final_table[final_table.loc[:,'Base_Price'] < final_table.loc[:, 'Rate']]

calculated_commission['final_com'] = cal_com.loc[:, 'Qty'] * cal_com.loc[:, 'Commission']][2]

customers = calculated_commission['Customer_Name'].unique()

for i in customers:
    a = calculated_commission[calculated_commission['Customer_Name'].str.match(i)]
    a.to_excel(i+'.xlsx')

I'm trying to iterate over unique customer names and write them in different excel files and name that with the same name.我正在尝试遍历唯一的客户名称并将它们写入不同的 excel 文件并使用相同的名称命名。

It created both files but writes data only on the second file which is 'Chef Themiya'它创建了两个文件,但只在第二个文件“Chef Themiya”上写入数据

access below link for the dataset:访问以下数据集链接:

https://drive.google.com/file/d/1VWc_WoN1nTWiDKK1YDtIXtzaAGdgbfpl/view?usp=sharing https://drive.google.com/file/d/1VWc_WoN1nTWiDKK1YDtIXtzaAGdgbfpl/view?usp=sharing

Please help请帮忙

str.match isnt picking up the pattern for customer name since your string has parenthesis in it. str.match 没有选择客户名称的模式,因为您的字符串中有括号。 Use this instead.改用这个。

df = pd.DataFrame({'Customer': ['88 Chinese Restaurant (Pvt) Ltd', 'Chef Themiya'], 'data': [1,2]})
customers = df['Customer'].unique()
for i in customers:
    test = df[df['Customer']== i]
    print(test)

you could also use你也可以使用

df = pd.DataFrame({'Customer': ['88 Chinese Restaurant (Pvt) Ltd', 'Chef Themiya'], 'data': [1,2]})
customers = df['Customer'].unique()
for i in customers:
    test = df[df['Customer'].str.contains(i, regex=False)]
    print(test)
       Customer  data
0  88 Chinese Restaurant (Pvt) Ltd     1

       Customer  data
1  Chef Themiya     2

Posting your dataset as images makes your example difficult to reproduce.将您的数据集作为图像发布会使您的示例难以重现。 Look into creating minimal reproducable examples.研究创建最小的可复制示例。

https://stackoverflow.com/help/minimal-reproducible-example https://stackoverflow.com/help/minimal-reproducible-example

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

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