如何将字典和数据帧一起写入excel

时间:2016-08-30 13:54:44

标签: excel pandas dictionary

我有一本定义客户的字典:

Customer
    role:blah
    phonenumber:111-222-3333
    company:blahblah
    branch:blahblahblah

我有一个包含大约10列信息的所有送货信息的数据框。我想要完成的是首先在工作表中写入我已匹配的客户信息,然后在其下面写入带有标题的数据框,然后为最后一个下面的客户执行相同的操作。每个工作表都由角色定义,因此在同一工作表上显示的客户不多。

我想出了如何根据角色工作表将数据帧写入工作簿,但我无法打印出它上面的字典。你能帮助我吗?

# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter('pandas_simple.xlsx', engine='xlsxwriter')
row_count = dict()    
for ID in df['PhoneNumber']:
    sheet_name_var = str(self.match_ID2role(ID))
    #count the row position for each df in each sheet
    df_length = len(df)
    try:
        row_count[sheet_name_var]+=1
    except:
        row_count[sheet_name_var]=1
    #add the customer dictionary somehow
    #####*****************#########
    row_count[sheet_name_var]+=1
    # Convert the dataframe to an XlsxWriter Excel object.
    df.to_excel(writer, sheet_name= sheet_name_var,startrow=row_count[sheet_name_var])

输出示例:

James
    role: blah
    phonenumber:999-888-7777
    company: blahblah
    branch: blahblahblah

Timestamp    PhoneNumber    Call_duration    Product   
2016-01-01   999-888-7777   12.34            beachballs

Bobby
    role: bla
    phonenumber: 666-444-5555
    company: blebla
    branch: bleblabloop

Timestamp    PhoneNumber    Call_duration    Product   
2016-01-01   666-444-5555   35.24            applies 

0 个答案:

没有答案