【问题标题】:Pandas concat from excel tables with missing data缺少数据的 Excel 表中的 Pandas concat
【发布时间】:2015-04-01 17:25:26
【问题描述】:

我正在尝试连接一些我从 excel 中读取并使用 pandas 旋转(堆叠)的表。除了最后的数据框(GRL)缺少最后两列之外,下面的代码运行良好。这些列对应于输入 excel 文件中的列,其中有几个空单元格作为第一个条目。我尝试在数据子集(和测试数据)上使用 concat ,它似乎有效,但不适用于整个数据集。似乎 pd.concat 没有对列以缺失数据开头的日期时间索引进行联合。

谁能在这里发现一个明显的错误?谢谢。

path = 'C:\\dr\\'
WQ_file = 'File.xlsx'

xl = pd.ExcelFile(path + WQ_file)
sheet_names = xl.sheet_names

GRL = pd.read_excel(path + WQ_file, sheetname = sheet_names[0], parse_cols = 15, index_col = 0, na_values = ['', 'na'])
GRL = GRL.stack(dropna=False).reorder_levels([1,0]).sortlevel(0)
GRL.name = sheet_names[0]

for n in sheet_names[1:-1]:
    df1 = pd.read_excel(path + WQ_file, sheetname = n, parse_cols = 15, index_col = 0, na_values = ['', 'na'])
    df1 = df1.stack(dropna=False).reorder_levels([1,0]).sortlevel(0)
    df1.name = n
    GRL = pd.concat([GRL, df1], axis = 1)
GRL.columns.name = 'Locations'
GRL

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    这确实有效。所以没有问题。谢谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-14
      • 2020-06-08
      • 2018-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-26
      相关资源
      最近更新 更多