Python Pandas dataframe reading exact specified range in an excel sheet

后端 未结 3 1153
北海茫月
北海茫月 2020-12-28 19:17

I have a lot of different table (and other unstructured data in an excel sheet) .. I need to create a dataframe out of range \'A3:D20\' from \'Sheet2\' of Excel sheet \'data

3条回答
  •  礼貌的吻别
    2020-12-28 19:55

    One way to do this is to use the openpyxl module.

    Here's an example:

    from openpyxl import load_workbook
    
    wb = load_workbook(filename='data.xlsx', 
                       read_only=True)
    
    ws = wb['Sheet2']
    
    # Read the cell values into a list of lists
    data_rows = []
    for row in ws['A3':'D20']:
        data_cols = []
        for cell in row:
            data_cols.append(cell.value)
        data_rows.append(data_cols)
    
    # Transform into dataframe
    import pandas as pd
    df = pd.DataFrame(data_rows)
    

提交回复
热议问题