熊猫:读取excel文件中的特定列,删除空白行,创建CSV

时间:2019-12-14 15:42:17

标签: python excel pandas loops csv

我正在尝试读取.xlsm文件的文件夹,采用A:J列,删除所有空行,然后将每个Excel文件合并为一个CSV。当我仅使用一个特定的excel文件时,下面的代码似乎可以工作,但是在循环时出现错误。任何帮助,将不胜感激。

import pandas as pd
import os
import glob

# defines the folder to pull from and to save into
source = r"C:\Users\bwendt\QAR" 

#defines list of files as dir and changes directory to source
os.chdir(source) 
files = glob.glob(source + "/*.xlsm")
MultiRents = []

#loops through list of file paths, reads the file, removes blank cells, and adds to data frame
for f in files:
   data = pd.read_excel(f,"Page2",usecols = "A:J")
   #data.dropna(axis=0, how='any', thresh=None, subset=None, inplace=True)
   MultiRents.append(data)

#create pandas DF
df = pd.DataFrame.from_records(MultiRents)

#Exports dataframe to a csv file
export_csv = df.to_csv("Multifamily_Rents.csv")
  

回溯:回溯(最近一次通话结束):

     

文件“”,第1行,在       runfile('C:/Users/bwendt/.spyder-py3/Print_rents.py',wdir ='C:/Users/bwendt/.spyder-py3')

     

文件   “ C:\ Users \ bwendt \ AppData \ Local \ Continuum \ anaconda3 \ lib \ site-packages \ spyder_kernels \ customize \ spydercustomize.py”,   运行文件中的第827行       execfile(文件名,命名空间)

     

文件   “ C:\ Users \ bwendt \ AppData \ Local \ Continuum \ anaconda3 \ lib \ site-packages \ spyder_kernels \ customize \ spydercustomize.py”,   第110行,在execfile中       exec(compile(f.read(),文件名,'exec'),命名空间)

     

文件“ C:/Users/bwendt/.spyder-py3/Print_rents.py”,第21行,在          数据= pd.read_excel(f,“ Page2”,usecols =“ A:J”)

     

文件   “ C:\ Users \ bwendt \ AppData \ Local \ Continuum \ anaconda3 \ lib \ site-packages \ pandas \ util_decorators.py”,   包装中的第188行       返回func(* args,** kwargs)

     

文件   “ C:\ Users \ bwendt \ AppData \ Local \ Continuum \ anaconda3 \ lib \ site-packages \ pandas \ util_decorators.py”,   包装中的第188行       返回func(* args,** kwargs)

     

文件   “ C:\ Users \ bwendt \ AppData \ Local \ Continuum \ anaconda3 \ lib \ site-packages \ pandas \ io \ excel.py”,   第350行,在read_excel中       io = ExcelFile(io,engine = engine)

     

文件   “ C:\ Users \ bwendt \ AppData \ Local \ Continuum \ anaconda3 \ lib \ site-packages \ pandas \ io \ excel.py”,    init 中的第653行       self._reader = self._enginesengine

     

文件   “ C:\ Users \ bwendt \ AppData \ Local \ Continuum \ anaconda3 \ lib \ site-packages \ pandas \ io \ excel.py”,   第424行,在 init 中       self.book = xlrd.open_workbook(filepath_or_buffer)

     

文件   “ C:\ Users \ bwendt \ AppData \ Local \ Continuum \ anaconda3 \ lib \ site-packages \ xlrd__init __。py”,   第157行,在open_workbook中       ragged_rows = ragged_rows,

     

文件   “ C:\ Users \ bwendt \ AppData \ Local \ Continuum \ anaconda3 \ lib \ site-packages \ xlrd \ book.py”,   第92行,在open_workbook_xls中       biff_version = bk.getbof(XL_WORKBOOK_GLOBALS)

     

文件   “ C:\ Users \ bwendt \ AppData \ Local \ Continuum \ anaconda3 \ lib \ site-packages \ xlrd \ book.py”,   1278行,在getbof中       bof_error('期望的BOF记录;找到%r'%self.mem [savpos:savpos + 8])

     

文件   “ C:\ Users \ bwendt \ AppData \ Local \ Continuum \ anaconda3 \ lib \ site-packages \ xlrd \ book.py”,   第1272行,在bof_error中       引发XLRDError(“格式不受支持,或文件损坏:'+ msg)

     

XLRDError:不支持的格式,或文件损坏:预期的BOF记录;   找到了b'\ x0eWendt,'

0 个答案:

没有答案
相关问题