【发布时间】:2018-02-08 20:31:25
【问题描述】:
[迷你康达,python 3]
我要下载的数据 .xls:(密码:stack) Download .xls
0) 您会注意到我的 xls 文件在第一行有很大的合并单元格,在第 2 行和第 3 行也有一些合并单元格。这是一个问题吗?如果这是一个问题 - 我可以以某种方式取消它们吗?
1) 我想删除此 xls 的第一行,因为对我来说没有重要信息。我想问题是该行被合并了?我想为此使用 df = df.drop([0]) ,但不是删除这个巨大的第一行,而是删除带有列标题的行(以“ID klienta”开头)。这是为什么呢?
2) 在我摆脱第一行之后,我喜欢处理来自不同列的一些数字(在我的示例中,我想将数据从“Stav”列中分离出来)。我怎么做?我在某处看到可以仅通过其标题名称(字符串)来索引行/列。例如,我想使用标题“Stav”从列中分离数据:Stav = df['Stav']
到目前为止我的代码是:
import pandas as pd
import numpy as np
print("\n\n*********************************************")
print("My xls processing script\n")
print("*********************************************\n")
#load data
df = pd.read_excel("file.xls")
#My unsucessful attempt to get rid of first row
#uncomment this and it will remove the second row instead of the first row
#df = df.drop([0])
#print preview of 6 rows 5 columnts
print(df.iloc[0:5, 0:4])
print("\n\n")
#My unsuccessful attempt to get column date with header 'ID'
Stav = df['Stav']
print(Stav)
控制台输出:
(xls_env) C:\Users\Slavek\Documents\PythonScripts>python xld_proj.py
*********************************************
My xls processing script
*********************************************
Lidé, které jsem podpořil Unnamed: 1 Unnamed: 2 Unnamed: 3
0 ID klienta Název Stav ID příběhu
1 NaN NaN NaN NaN
2 zonky214882 Jeep na cestě 181187
3 zonky235862 Notebook k práci i relaxu na cestě 206317
4 zonky230378 Dětský pokoj v pořádku 199686
Traceback (most recent call last):
File "C:\miniconda\envs\xls_env\lib\site-packages\pandas\core\indexes\base.py", line 2525, in get_loc
return self._engine.get_loc(key)
File "pandas/_libs/index.pyx", line 117, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/index.pyx", line 139, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 1265, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 1273, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'Stav'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "xld_proj.py", line 20, in <module>
Stav = df['Stav']
File "C:\miniconda\envs\xls_env\lib\site-packages\pandas\core\frame.py", line 2139, in __getitem__
return self._getitem_column(key)
File "C:\miniconda\envs\xls_env\lib\site-packages\pandas\core\frame.py", line 2146, in _getitem_column
return self._get_item_cache(key)
File "C:\miniconda\envs\xls_env\lib\site-packages\pandas\core\generic.py", line 1842, in _get_item_cache
values = self._data.get(item)
File "C:\miniconda\envs\xls_env\lib\site-packages\pandas\core\internals.py", line 3843, in get
loc = self.items.get_loc(item)
File "C:\miniconda\envs\xls_env\lib\site-packages\pandas\core\indexes\base.py", line 2527, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas/_libs/index.pyx", line 117, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/index.pyx", line 139, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 1265, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 1273, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'Stav'
【问题讨论】:
-
亲爱的先生,我应该删除屏幕的sn-ps吗?我可以做到这一点没问题。
-
获得 401 未授权
-
你是对的,链接不再有效。我附上了excel的sn-ps,但版主告诉我这是不可接受的形式,所以我不得不删除它们。下班回家后,我将再次附上示例 xls。非常感谢这个社区迄今为止所做的努力!
-
下载示例 file.xls 现在应该可以工作了。欢迎下载。