【问题标题】:Appending Columns from several worksheets Python附加来自多个工作表的列 Python
【发布时间】:2018-11-24 06:33:17
【问题描述】:

我正在尝试从工作簿内的多个不同工作表中导入某些数据列。但是,虽然附加它似乎只是将“q2 调查”附加到新工作簿。如何让它正确附加?

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)

【问题讨论】:

    标签: excel python-2.7 pandas xlrd xlwt


    【解决方案1】:

    我想如果你这样做:

    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)
    

    【讨论】:

      猜你喜欢
      • 2021-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-10
      相关资源
      最近更新 更多