pandas读取excel文件指定列

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pandas读取excel文件指定列相关的知识,希望对你有一定的参考价值。

读取Excel文件,指定列需要在软件中点击文档表格插入开启。
然后找到信息指定功能。
选定文件进行文件的行列,对比标定列项来进行读取。
参考技术A PANDEAS读取的Excel文件指定列,这个应该是可以进行读取的。 参考技术B 你好关于这个词语,建议你采用Excel的find查找。

无法使用 read_excel 从 pandas 中的 xlsx 文件中读取日期列?

【中文标题】无法使用 read_excel 从 pandas 中的 xlsx 文件中读取日期列?【英文标题】:Unable to get date column to read from xlsx file in pandas using read_excel? 【发布时间】:2020-02-12 09:49:56 【问题描述】:

我在 Excel 中有格式为日期的单元格(见下文):

我无法读取它们(它们是 NaN),因此当从 Pandas read_excel 方法读取时,我使用转换器尝试转换它们 to_datetime

   cols_A8_J2007[i] = pd.read_excel(
                      i, 
                     ('sheet'+str(j)), 
                     headers = 1, skiprows = 6, nrows=2000, 
                     usecols = 'A:J', 
                     converters = 
                       'Expired': lambda x: pd.to_datetime(x, errors='coerce') , 
                   'Valid Until': lambda x: pd.to_datetime(x, errors='coerce'))

这导致它们都被加载为NaT

所以,在查阅了文档后,我尝试了这种方式:

    cols_A8_J2007[i] = pd.read_excel(i, ('sheet'+str(j)), headers = 1, parse_dates=True, skiprows = 6, nrows=2000, usecols = 'A:J' )

这又导致了NaN

最后我这样尝试了,又得到了NaN

    cols_A8_J2007[i] = pd.read_excel(i, ('sheet'+str(j)), headers = 1, parse_dates=True, date_parser=lambda x: pd.to_datetime(x, errors='coerce'), skiprows = 6, nrows=2000, usecols = 'A:J' )

上述方法不起作用,因为它尝试根据索引进行解析(请参阅下面的评论)。

cols_A8_J2007[i] = pd.read_excel(i, ('sheet'+str(j)), headers = 1, parse_dates=['Expired', 'Valid Until'], skiprows = 6, nrows=2000, usecols = 'A:J' )
cols_A8_J2007[i] = pd.read_excel(i, ('sheet'+str(j)), headers = 1, parse_dates=['Expired', 'Valid Until'], skiprows = 6, nrows=2000, usecols = 'A:J' )
cols_A8_J2007[i] = pd.read_excel(i, ('sheet'+str(j)), headers = 1, parse_dates=['Expired', 'Valid Until'], dateparser=lambda x: pd.to_datetime(x, errors='coerce'), skiprows = 6, nrows=2000, usecols = 'A:J' )

这两个结果都导致NaT(不是时间?)

我还需要做什么才能读取日期?我意识到没有附加时间,但是Excel stores dates and times 的方式并不重要,因为时间存储为小数。

for i in glob.iglob(((str(xls_folder) + '\somesheets*.xlsx'))):
    cols_A8_J2007[i] = pd.read_excel(i, ('sheet'+str(j)), headers = 1, skiprows = 6, nrows=2000, usecols = 'A:J', converters = 'Expired': lambda x: pd.to_datetime(x, errors='coerce') , 'Valid Until': lambda x: pd.to_datetime(x, errors='coerce'))

for w in cols_A8_J2007:
    print(cols_A8_J2007[w].dtypes)

Type                      object
Currency                  object
Initial Credit           float64
Credits                  float64
Debits                   float64
Balance                  float64
Reserved                   int64
Valid Until       datetime64[ns] <-  <- These I believe are what you are looking for..
Expired           datetime64[ns] <- These I believe are what you are looking for..
dtype: object

如果这有帮助,这里是我的版本:

pd.versions()
INSTALLED VERSIONS
------------------
commit: None
python: 3.7.3.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 158 Stepping 9, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None

pandas: 0.24.2
pytest: 4.5.0
pip: 19.1.1
setuptools: 41.0.1
Cython: 0.29.8
numpy: 1.16.4
scipy: 1.2.1
pyarrow: None
xarray: None
IPython: 7.5.0
sphinx: 2.0.1
patsy: 0.5.1
dateutil: 2.8.0
pytz: 2019.1
blosc: None
bottleneck: 1.2.1
tables: 3.5.1
numexpr: 2.6.9
feather: None
matplotlib: 3.0.3
openpyxl: 2.6.2
xlrd: 1.2.0
xlwt: 1.3.0
xlsxwriter: 1.1.8
lxml.etree: 4.3.3
bs4: 4.7.1
html5lib: 1.0.1
sqlalchemy: 1.3.3
pymysql: None
psycopg2: None
jinja2: 2.10.1
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
gcsfs: None

【问题讨论】:

parse_dates=True 解析索引。 parse_dates=['col1','col2'] 解析列出的列。 @QuangHoang 这又给了我NaT,我在有/没有date_parser=lambda x: pd.to_datetime(x, errors='coerce')的情况下都试过了。 您总是可以将它们作为字符串读取,然后转换为正确的格式,但这对您来说可能不合理.. @leeand00 当您更改格式时 - 您是否重新保存了 excel 文件? @JonClements 是的;其中有 7 个。 【参考方案1】:

确定问题是我需要使用pd.isnull 检查值是否为空。该文件的空值太多,我无法在结果集中看到它们。在这里找到答案:how to test if a variable is pd.NaT?

me_df = pd.read_excel(i, ('iCareAcctListing'+str(j)), headers = 1, skiprows = 6, nrows=2000, usecols = 'A:J', converters = 'Expired': lambda x: pd.to_datetime(x, errors='coerce') , 'Valid Until': lambda x: pd.to_datetime(x, errors='coerce'))

# Ran this and ended up with just the dates that were 
# filled in with actual values.
#
# There were so many nulls before and after that I couldn't see any of them in the dataset!
me_df[pd.isnull(me_df['Valid Until']) != True]

【讨论】:

以上是关于pandas读取excel文件指定列的主要内容,如果未能解决你的问题,请参考以下文章

pandas从excel读取数据数字类型过长出现科学计数法的问题

pandas 读取excel的指定列

pandas读取和写入excel,csv太慢怎么办

pandas读取和写入excel,csv太慢怎么办

Python:当文件在列标题中有特殊字符时,使用 Pandas 读取 Excel 文件

无法使用 read_excel 从 pandas 中的 xlsx 文件中读取日期列?