Excel 从多个工作表中追加列
我试图从工作簿中的几个不同工作表中导入某些列的数据。然而,在附加时,它似乎只是将“第二季度调查”附加到新工作簿中。我怎样才能正确地附加它Excel 从多个工作表中追加列,excel,python-2.7,pandas,xlrd,xlwt,Excel,Python 2.7,Pandas,Xlrd,Xlwt,我试图从工作簿中的几个不同工作表中导入某些列的数据。然而,在附加时,它似乎只是将“第二季度调查”附加到新工作簿中。我怎样才能正确地附加它 import sys, os import pandas as pd import xlrd import xlwt b = ['q1 survey', 'q2 survey','q3 survey'] #Sheet Names df_t = pd.DataFrame(columns=["Month","Date", "Year"]) #column Na
import sys, os
import pandas as pd
import xlrd
import xlwt
b = ['q1 survey', 'q2 survey','q3 survey'] #Sheet Names
df_t = pd.DataFrame(columns=["Month","Date", "Year"]) #column Name
xls = "path_to_file/R.xls"
sheet=[]
df_b=pd.DataFrame()
pd.read_excel(xls,sheet)
for sheet in b:
df=pd.read_excel(xls,sheet)
df.rename(columns=lambda x: x.strip().upper(), inplace=True)
bill=df_b.append(df[df_t])
bill.to_excel('Survey.xlsx', index=False)
我认为如果你这样做:
b = ['q1 survey', 'q2 survey','q3 survey'] #Sheet Names
list_col = ["Month","Date", "Year"] #column Name
xls = "path_to_file/R.xls"
#create the empty df named bill to append after
bill= pd.DataFrame(columns = list_col)
for sheet in b:
# read the sheet
df=pd.read_excel(xls,sheet)
df.rename(columns=lambda x: x.strip().upper(), inplace=True)
# need to assign bill again
bill=bill.append(df[list_col])
# to excel
bill.to_excel('Survey.xlsx', index=False)
它应该可以工作并纠正代码中的错误,但是您可以使用pd.concat
:
list_sheet = ['q1 survey', 'q2 survey','q3 survey'] #Sheet Names
list_col = ["Month","Date", "Year"] #column Name
# read once the xls file and then access the sheet in the loop, should be faster
xls_file = pd.ExcelFile("path_to_file/R.xls")
#create a list to append the df
list_df_to_concat = []
for sheet in list_sheet :
# read the sheet
df= pd.read_excel(xls_file, sheet)
df.rename(columns=lambda x: x.strip().upper(), inplace=True)
# append the df to the list
list_df_to_concat.append(df[list_col])
# to excel
pd.concat(list_df_to_concat).to_excel('Survey.xlsx', index=False)