如何将列出的熊猫数据集写入excel xlsx文件?

时间:2019-09-18 00:23:47

标签: python pandas xlsx xlsxwriter

我正在尝试为列出的数据集创建一个if语句,以检查if语句中的条件,如果通过,则应在第一个已创建和格式化的工作表(从row [2]和column [0],如果未通过,则应在第二行已创建和格式化的工作表中将值添加到同一行和列的索引中

我添加了两个for循环以遍历数据

import csv
import xlsxwriter
import pandas
import pandas as pd
from openpyxl import load_workbook

with open(r'directory', 'r') as csvfile:
spamreader = csv.reader(csvfile)
newlist= list(spamreader)
#print(newlist)
#newlist = [['id', 'name', 'lastContactedTime', 'email', 'phone_phones', 'home_phones', 'mobile_phones', 'work_phones', 'fax_phones', 'other_phones', 'address_1', 'address_2', 'address_3', 'city', 'state', 'postal_code', 'country', 'tags'], ['15-contacts', 'Jane Doe', '', 'j@greenbriar.com', '', '+1 6570987234', '+1 3245687907', '+1 5678743546', '', '', '54 George street', '', '', 'Ridge Springs', 'VA', '25678', 'US', ''], ['3-contacts', 'Joe Malcoun', '2019-08-28 14:29:27', 'ceo@nutshell.com', '', '', '', '', '', '', '212 South Fifth Ave', '', '', 'Ann Arbor', 'MI', '48103', 'US', ''], ['7-contacts', 'Morgan Ramirez', '', 'mramirez@nerdy.com', '', '', '+1 6780431874', '+1 3338765438', '+1 5679876987', '', '567 one st', '', '', 'Birmingham', 'AL', '45678', 'US', ''], ['11-contacts', 'Roman Burki', '', 'burki@bvb.com', '', '', '+1 0983457690', '+1 5468756098', '', '', '546 fourteen street Nw', '', '', 'Dallas', 'TX', '54678', 'US', '']]




# I have created two worksheets, formatted them (code not included but worksheet works and added words to the cells

workbook= xlsxwriter.Workbook('Test1.xlsx')
worksheet= workbook.add_worksheet()

workbook2= xlsxwriter.Workbook('Test2.xlsx')
worksheet2= workbook2.add_worksheet()
df = pd.DataFrame(newlist[1:], columns=newlist[0])
addresses = df.address_1.tolist()


df[['name', 'id']] = df.name.str.split(' ', expand=True)

# rename id
df.rename(columns={'id': 'Lastname','name': 'Firstname','lastContactedTime': 'Company','email':'Work_email', 'other_phones':'Personal_City', 'address_1':'Work_Street', 'address_2':'Personal_Zip', 'address_3':'Personal_Street', 'city':'Work_City', 'state':'Work_State', 'postal_code':'Work_Zip', 'tags':'Personal_email'}, inplace=True)

df[['Work_email', 'Company']] = df.Work_email.str.split('@', expand=True)

df.Company = [x.strip('.com') for x in df.Company]


del df['phone_phones']
note =  [' ', ' ', ' ',' ']
df['Note'] = note


notecat =  [' ', ' ', ' ',' ']
df['Note_Category'] = notecat

title =  [' ', ' ', ' ',' ']
df['Title'] = title

Willingtoshare =  [' ', ' ', ' ',' ']
df['Willing_to_share'] = Willingtoshare

Willingtointroduce =  [' ', ' ', ' ',' ']
df['Willing_to_introduce'] = Willingtointroduce

Personalstate =[' ', ' ', ' ',' ']
df['Personal_State'] = Personalstate

df[['country_code', 'work_phones']] = df.work_phones.str.split(' ', expand=True)
del df['country_code']
del df['country']
del df['fax_phones']
del df['home_phones']
#print(df)

  Lastname Firstname  ... Willing_to_introduce Personal_State
0      Doe      Jane  ...                                    
1  Malcoun       Joe  ...                                    
2  Ramirez    Morgan  ...                                    
3    Burki     Roman  ...                                    

[4 rows x 20 columns]
excel_file = IO()

df = df[['Lastname', 'Firstname','Company','Title','Willing_to_share','Willing_to_introduce','work_phones','Work_email','Work_Street','Work_City','Work_State','Work_Zip','Personal_Street','Personal_City','Personal_State','Personal_Zip','mobile_phones','Personal_email','Note','Note_Category']]
data=df.values.tolist()
columns=df.columns
sf=df.columns.tolist()

book = load_workbook('Test1.xlsx')
writer= pd.ExcelWriter('Test1.xlsx',engine='xlsxwriter') 
writer.book = book


book2 = load_workbook('Test2.xlsx')
writer2= pd.ExcelWriter('Test2.xlsx',engine='xlsxwriter')
writer2.book2 = book2


workbook = writer.book
workbook2 = writer2.book2

worksheet = writer.sheets['Sheet1']
worksheet2 = writer.sheets['Sheet2']

value = data
for column in df:
   for col, sf in enumerate(data):
        if (column[0] != ' ' and column[1] != ' ') and ((column[2] != ' ' and column[3] != ' ') or (column[2] == ' ') and (column[6] != ' ') or (column[16] != ' ') and (column[12] != ' ' and column[13] != ' ' and column[14] != ' ' and column[15] != ' ') or (column[8] != ' ' and column[9] != ' ' and column[10] != ' ' and column[11] != ' ' and (column[7] != ' ' or column[17] != ' '))):
            worksheet.write('A3', value)
            #df.to_excel(writer, header = False, startcol=1, startrow=3)
        else:
            worksheet2.write('A3', value) 


writer.save()
writer.close()
writer2.save()
writer2.close()
workbook.close()
workbook2.close()

KeyError:“ Sheet1”,我的预期输出是它开始打印数据,而标题不从A3开始

2 个答案:

答案 0 :(得分:0)

编辑现有工作表的方式

import csv
import pandas as pd

with open(r'C:/workbench/SemiFinale/train.csv', 'r') as csvfile:
   spamreader = csv.reader(csvfile)
   newlist= list(spamreader)

df = pd.DataFrame(newlist[1:], columns=newlist[0])

# Creation of writer object 

writer= pd.ExcelWriter('Test1.xlsx',engine='xlsxwriter')

# This is first time creation and saving of file

df.to_excel(writer, header = False, sheet_name='Sheet1', startcol=5, startrow=10)  # startcol, startrow can be used in this condition
df.to_excel(writer, header = False, sheet_name='Sheet2', startcol=8, startrow=6)

#This is for reading of file, but first above code needs to be executed

workbook  = writer.book
worksheet1 = writer.sheets['Sheet1']
worksheet2 = writer.sheets['Sheet2']

value = "abc"
col_num = 10
worksheet1.write('A1', value)
workbook.close()


writer.save()
writer.close()

答案 1 :(得分:-1)

import csv
import pandas as pd

with open(r'C:/workbench/SemiFinale/train.csv', 'r') as csvfile:
   spamreader = csv.reader(csvfile)
   newlist= list(spamreader)

df = pd.DataFrame(newlist[1:], columns=newlist[0])

writer= pd.ExcelWriter('Test1.xlsx',engine='xlsxwriter')
df.to_excel(writer, header = False, sheet_name='Sheet1', startcol=1, startrow=10)  # startcol, startrow can be used in this condition
df.to_excel(writer, header = False, sheet_name='Sheet2', startcol=3, startrow=6)

# Above code successfully saves dataframe values in excel format
# And to satisfy your if condition you can use it as below format

# for column in df:
#    for col, sf in enumerate(data):
#       if (condition staisfied):
#          df.to_excel(writer, header = False, sheet_name='Sheet1', startcol=1, startrow=10)
#       else:
#          df.to_excel(writer, header = False, sheet_name='Sheet2', startcol=3, startrow=6)

writer.save()
writer.close()