gpt4 book ai didi

python - 只读某些列

转载 作者:太空宇宙 更新时间:2023-11-04 00:26:25 43 4
gpt4 key购买 nike

我只想将某些列从 excel 文件读取到 pandas 数据框中。我想通过 usecols 的列范围来指定它来指定它,但是我遇到了一个错误。将列范围指定为字母的正确方法是什么(是的,我知道我可以提供列索引)?

import pandas as pd
df = pd.read_excel("test.xlsx", usecols="A:AV", header=None)

~/anaconda3/lib/python3.5/site-packages/pandas/io/excel.py in read_excel(io, sheetname, header, skiprows, skip_footer, index_col, names, parse_cols, parse_dates, date_parser, na_values, thousands, convert_float, has_index_names, converters, dtype, true_values, false_values, engine, squeeze, **kwds) 207 skip_footer=skip_footer, converters=converters, dtype=dtype, 208 true_values=true_values, false_values=false_values, squeeze=squeeze, --> 209 **kwds) 210 211

~/anaconda3/lib/python3.5/site-packages/pandas/io/excel.py in _parse_excel(self, sheetname, header, skiprows, names, skip_footer, index_col, has_index_names, parse_cols, parse_dates, date_parser, na_values, thousands, convert_float, true_values, false_values, verbose, dtype, squeeze, **kwds) 508 squeeze=squeeze, 509 dtype=dtype, --> 510 **kwds) 511 512 output[asheetname] = parser.read()

~/anaconda3/lib/python3.5/site-packages/pandas/io/parsers.py in TextParser(*args, **kwds) 1910 """ 1911 kwds['engine'] = 'python' -> 1912 return TextFileReader(*args, **kwds) 1913 1914

~/anaconda3/lib/python3.5/site-packages/pandas/io/parsers.py in init(self, f, engine, **kwds) 762 self.options['has_index_names'] = kwds['has_index_names'] 763 --> 764 self._make_engine(self.engine) 765 766 def close(self):

~/anaconda3/lib/python3.5/site-packages/pandas/io/parsers.py in _make_engine(self, engine) 993 ' "c", "python", or' ' "python-fwf")'.format( 994 engine=engine)) --> 995 self._engine = klass(self.f, **self.options) 996 997 def _failover_to_python(self):

~/anaconda3/lib/python3.5/site-packages/pandas/io/parsers.py in init(self, f, **kwds) 1994 # infer column indices from self.usecols if is is specified. 1995 self._col_indices = None -> 1996 self.columns, self.num_original_columns = self._infer_columns() 1997 1998 # Now self.columns has the set of columns that we will process.

~/anaconda3/lib/python3.5/site-packages/pandas/io/parsers.py in _infer_columns(self) 2387 else: 2388 columns = [lrange(ncols)] -> 2389 columns = self._handle_usecols(columns, columns[0]) 2390 else: 2391 if self.usecols is None or len(names) >= num_original_columns:

~/anaconda3/lib/python3.5/site-packages/pandas/io/parsers.py in _handle_usecols(self, columns, usecols_key) 2422 for col in self.usecols: 2423 if isinstance(col, string_types): -> 2424 col_indices.append(usecols_key.index(col)) 2425
else: 2426 col_indices.append(col)

In [20]: pd.__version__
Out[20]: '0.20.2'

文件:test.xlsx

最佳答案

需要升级到 pandas 0.21.0 ( dev discussion ):

df = pd.read_excel("test.xlsx", usecols='A:B', header=None)                              
print (df.head())

0 1
0 sequence 2015-02-11 00:00:00
1 Aa 6239
2 AaBpL5 NaN
3 AaCjL5 NaN
4 AaDrL2 NaN

关于python - 只读某些列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47302328/

43 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com