繁体   English   中英

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

[英]Writing pandas dataframes to multiple excel files

图像显示了一个熊猫数据框

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

我正在尝试遍历唯一的客户名称并将它们写入不同的 excel 文件并使用相同的名称命名。

它创建了两个文件,但只在第二个文件“Chef Themiya”上写入数据

访问以下数据集链接:

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

请帮忙

str.match 没有选择客户名称的模式,因为您的字符串中有括号。 改用这个。

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)

你也可以使用

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

将您的数据集作为图像发布会使您的示例难以重现。 研究创建最小的可复制示例。

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

暂无
暂无

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

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