简体   繁体   English

将多个 excel 电子表格中的特定单元格读取到单个 pandas dataframe

[英]read a specific cells from multiple excel spreadsheets into single pandas dataframe

I would like to read specific cells from multiple excel spreadsheets into single pandas dataframe.我想将多个 excel 电子表格中的特定单元格读取到单个 pandas dataframe 中。

so far, I have tried this.到目前为止,我已经尝试过了。 (without a success) (没有成功)

import pandas as pd
import glob
import xlrd

file_list = glob.glob("*.xls")

df = pd.DataFrame()

for f in file_list:
    wb = xlrd.open_workbook(f)
    sheet = wb.sheet_by_index(0)
    name = sheet.cell_value(rowx=9, colx=2)
    city = sheet.cell_value(rowx=15, colx=2)
    df = df.append([name,city])

Desired output is pandas dataframe as this所需的 output 是 pandas dataframe

name   city
Tom    NY
Alex   Toronto
Anne   Atlanta
...    ...

Thanks谢谢

I think you need two sets of [[]] around what is being appended.我认为你需要两组[[]]围绕正在附加的内容。 With one set of brackets, it tries to add name as a row and city as a row, rather than as columns in the same row.使用一组括号,它尝试将名称添加为一行,将城市添加为一行,而不是作为同一行中的列。

import pandas as pd
import glob
import xlrd

file_list = glob.glob("*.xls")

df = pd.DataFrame()

for f in file_list:
    wb = xlrd.open_workbook(f)
    sheet = wb.sheet_by_index(0)
    name = sheet.cell_value(rowx=9, colx=2)
    city = sheet.cell_value(rowx=15, colx=2)
    df = df.append([[name,city]])

This will have columns named 0 and 1 , though (since you didn't define names in the creation of the dataframe), so a last step would be to rename those:不过,这将有名为01的列(因为您没有在创建数据框时定义名称),所以最后一步是重命名这些:

df = df.rename(columns={0:'name',1:'city'})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 分析从Excel到Dataframe Pandas的单个单元格 - Analyzing single cells from Excel to Dataframe Pandas 如何将Excel工作簿中的多个电子表格合并为熊猫数据框? - How to concat multiple spreadsheets in Excel workbooks into pandas dataframe? 将多个Excel工作簿中的多个工作表合并到一个Pandas数据框中 - Merge multiple sheets from multiple Excel workbooks into a single Pandas dataframe 将多个工作簿中的单个Excel工作表捕获到熊猫数据框中,并将其保存 - Grabbing a single Excel worksheet from multiple workbooks into a pandas dataframe and saving this 如何使用 Pandas 将 csv 中的多行读取到单个数据帧行中 - How to read multiple lines from csv into a single dataframe row with pandas 将具有空白单元格的Excel文件读取为具有multiindex的Pandas数据框 - Read Excel file with blank cells as Pandas dataframe with multiindex 如何使用NaN将合并的Excel单元格读入Pandas DataFrame - How to read merged Excel cells with NaN into Pandas DataFrame 如何取消合并多个单元格并将每个值转置到 excel 文件中的 Pandas dataframe 中的新列 - How to unmerge multiple cells and transpose each value into a new column in Pandas dataframe from excel file 从 excel 读取日期到 Pandas Dataframe - Read dates from excel to Pandas Dataframe 将从Excel读取的数据组织到Pandas DataFrame - Organizing data read from Excel to Pandas DataFrame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM