【问题标题】:Python can't convert list into DataFramePython 无法将列表转换为 DataFrame
【发布时间】:2020-03-22 04:24:20
【问题描述】:

我尝试从同一个 excel 工作集中导入 2 组数据,它们位于不同的范围内。 首先,我具有允许我输入范围以从范围中提取数据的功能。然后我想将每个范围数据附加为 1 个 DataFrame。该代码正在从提取数据到附加数据。 我已将其转换为 DataFrame 但它仍在列表中。当我导出到 excel 文件时,它只有 2 行数据,因为有 2 个子列表。 我的预期输出是 1 DataFrame not list,行和列中的数据。

#code to import workbook 
    from openpyxl import load_workbook
    from openpyxl.utils import get_column_interval
    import re

#function allow me to enter range 
    def load_workbook_range(range_string, ws):
      col_start, col_end = re.findall("[A-Z]+", range_string)

      data_rows = []
      for row in ws[range_string]:
          data_rows.append([cell.value for cell in row])

      return pd.DataFrame(data_rows, columns=get_column_interval(col_start, col_end))

# load workbook 
    wb = load_workbook(filename=b, read_only=True)
    ws = wb.active
     List = ['B24:D29','B2:D11'];

#create list for list and extra data from different range (List)
    df_list=[]
    for r in List:
      y=load_workbook_range(r,ws)
      f=pd.DataFrame(y)
      f.columns = f.iloc[0]
      g=f.drop(f.index[1])
      df_list.append(g)


    df_list

#output 

    [0  InComplete offer     NaN  Response 
     0  InComplete offer    None  Response 
     2                  1  name       Marie
     3                  2      a    13.7716
     4                  3      b    75.2104
     5                  4      c    26.8648,
     0  InComplete offer          NaN  Response 
     0  InComplete offer         None  Response 
     2                  1       name       Marie
     3                  2     invest     13.7716
     4                  3    capital     75.2104
     5                  4     income     26.8648
     6                  5    expense     83.1496
     7                  6     school     43.5749
     8                  7  transport     85.2239
     9                  8      others    82.3117]

#if I export to excel file, it is a list
    w = pd.DataFrame(df_list)
    w.to_excel (new_path, index = False, header=True)
    w

#output in excel file 

    0
    0   0 InComplete offer NaN Response 0 InCo...
    1   0 InComplete offer NaN Response 0 ...

#my expect output when export to excel file , look like below , in 3 different columns and multi rows.

    0  InComplete offer     NaN  Response 
     0  InComplete offer    None  Response 
     2                  1  name       Marie
     3                  2      a    13.7716
     4                  3      b    75.2104
     5                  4      c    26.8648,
     0  InComplete offer          NaN  Response 
     0  InComplete offer         None  Response 
     2                  1       name       Marie
     3                  2     invest     13.7716
     4                  3    capital     75.2104
     5                  4     income     26.8648
     6                  5    expense     83.1496
     7                  6     school     43.5749
     8                  7  transport     85.2239
     9                  8      others    82.3117

如果有人可以提供帮助并表示感谢。 MC

【问题讨论】:

    标签: python excel list dataframe


    【解决方案1】:

    试试

    i = 0
    for r in List:
    
        y = load_workbook_range(r,ws)
        f = pd.DataFrame(y)
        f.columns = f.iloc[0]
        g = f.drop(f.index[1])
    
        i += 1
        if i == 1:
           df_list = g
        else:
           df_list = df_list.append(g)
    

    【讨论】:

      猜你喜欢
      • 2016-12-01
      • 2019-06-24
      • 2022-01-07
      • 1970-01-01
      • 2022-01-23
      • 2022-11-16
      • 2021-01-08
      • 1970-01-01
      • 2021-03-01
      相关资源
      最近更新 更多