简体   繁体   中英

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.

It created both files but writes data only on the second file which is 'Chef Themiya'

access below link for the dataset:

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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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