Почему на этот раз я не могу выбрать один столбец из DataFrame путем печати (['column1'])? - PullRequest
0 голосов
/ 11 января 2019

Я могу выбрать один column из DataFrame, например: код как print(df['201809']) работает:

df = pd.read_csv('xxxx.csv', low_memory=False)

print(df.info()]
<class 'pandas.core.frame.DataFrame'>
Int64Index: 11 entries, 0 to 10
Data columns (total 4 columns):
BO_product2    11 non-null object
201808         11 non-null float64
201809         11 non-null float64
4              11 non-null float64
dtypes: float64(3), object(1)
memory usage: 440.0+ bytes 

print(df['201809'])      # works fine
    None
0     1.634931e+06
1     2.653640e+08
2     7.475315e+07
3     9.710830e+06
4     3.023899e+08
5     1.087862e+08
6     2.031106e+08
7     3.556234e+08
8     5.830665e+06
9     8.766841e+08
10    7.544689e+07
Name: 201809, dtype: float64

Однако print(df['4']) нет. Любые советы или идеи здесь?

PS: если я сохраню df.to_csv('yy.csv) в локальном файле в формате csv, print(a['4']) будет работать после `df = pd.read_csv ('yy.csv').

print(df['4'])
Traceback (most recent call last):
  File "C:\Users\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\indexes\base.py", line 3063, in get_loc
    return self._engine.get_loc(key)
  File "pandas\_libs\index.pyx", line 140, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 1492, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 1500, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: '4'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "E:/Python/2.py", line 45, in <module>
    he()
  File "E:/Python/2.py", line 26, in he
    print(a['4'])
  File "C:\Users\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\frame.py", line 2685, in __getitem__
    return self._getitem_column(key)
  File "C:\Users\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\frame.py", line 2692, in _getitem_column
    return self._get_item_cache(key)
  File "C:\Users\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\generic.py", line 2486, in _get_item_cache
    values = self._data.get(item)
  File "C:\Users\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\internals.py", line 4115, in get
    loc = self.items.get_loc(item)
  File "C:\Users\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\indexes\base.py", line 3065, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))
  File "pandas\_libs\index.pyx", line 140, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 1492, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 1500, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: '4'

1 Ответ

0 голосов
/ 11 января 2019

Если вы выполните следующее:

[type(i) for i in df.columns]
#[str, str, str, int]

Для столбцов типа int вы должны называть столбец как df[4], а не df['4']

Вероятно, причина, по которой он записывается в виде строки, связана со встроенной функцией quoting. Из документов:

цитирование: необязательная константа из модуля CSV

по умолчанию csv.QUOTE_MINIMAL. Если вы установили float_format, то числа с плавающей точкой >> преобразуются в строки, и поэтому csv.QUOTE_NONNUMERIC будет обрабатывать их как не - >> numeric

Надеюсь, это поможет.

...