我可能以错误的方式处理这件事.. 我正在尝试查找大约 100 家英国医院的邮政编码。我有一个 Excel 电子表格 (all_all),其中包含英国医院/诊所等的总数 (14,000) 及其地址和邮政编码。
我有一个按年份列出这 100 家医院手术事件的数据框(脊柱),其中医院名称在 2817 行中重复。
spine.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 2818 entries, 0 to 2817
Data columns (total 7 columns):
index_col 2818 non-null float64
fyear 2818 non-null int64
NNAPID 2818 non-null int64
mainspef 2818 non-null int64
Trust 2818 non-null object
complexcount 2818 non-null float64
simplecount 2818 non-null float64
dtypes: float64(3), int64(3), object(1)
memory usage: 154.2+ KB
我想我可以使用Pandas series map 。
导入 csv,包括所有 14,000 家医院。
postcodes_all = pd.read_csv('all_all.csv')
postcodes_all.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 14206 entries, 0 to 14205
Data columns (total 3 columns):
Unnamed: 0 14206 non-null int64
Trust_title 14206 non-null object
postcode 14206 non-null object
dtypes: int64(1), object(2)
memory usage: 333.1+ KB
在英国,医院是信托机构,因此在我的数据框(主干)中,医院名称列 = 信托机构。我正在尝试将其映射到 postcodes_all (Trust_title) 中的医院条目。
spine['Trust'].map(postcodes_all['Trust_title'])
0 NaN
1 NaN
2 NaN
3 NaN
4 NaN
...
2813 NaN
2814 NaN
2815 NaN
2816 NaN
2817 NaN
Name: Trust, Length: 2818, dtype: object
未找到任何匹配项。医院字段例如 LEEDS TEACHING HOSPITALS NHS TRUST,相同的条目位于 postcodes_all 中。
有没有办法探究失败的原因?我是一名医生,试图学习 python 和 pandas 进行数据分析,所以非常早期的步骤。
我不确定它是否没有失败,只是在某处定义了错误的数据类型,或者我正在尝试匹配本质上不相似的两列,并且希望能够检查我失败的代码。
由于我正赶去诊所,所以OP的含糊和简短对此表示抱歉。
更新。
根据下面乔的评论,我简化了事情。在脊柱 csv 中,我已将要使用的列定义为“信任”,在邮政编码 csv 中,我已将索引列定义为“信任”。
我现在确实将脊柱中的医院名称与邮政编码中的索引字段进行比较。我现在在“信任”中遇到了一个关键错误。
我的代码在这里
import pandas as pd
spine = pd.read_csv('~/Dropbox/Work/NNAP/Spine/Kate_W/kate_spine2.csv', usecols = ['Trust'])
spine.head()
Trust
0 THE WALTON CENTRE NHS FOUNDATION TRUST
1 CAMBRIDGE UNIVERSITY HOSPITALS NHS FOUNDATION ...
2 KING'S COLLEGE HOSPITAL NHS FOUNDATION TRUST
3 LEEDS TEACHING HOSPITALS NHS TRUST
4 NT424
postcodes_all = pd.read_csv('all_all.csv', index_col = 'Trust')
postcodes_all.head()
Unnamed: 0 postcode
Trust
MANCHESTER UNIVERSITY NHS FOUNDATION TRUST 0 M13 9WL
SOUTH TYNESIDE AND SUNDERLAND NHS FOUNDATION TRUST 1 SR4 7TP
WORCESTERSHIRE HEALTH AND CARE NHS TRUST 2 WR5 1JR
SOLENT NHS TRUST 3 SO19 8BR
SHROPSHIRE COMMUNITY HEALTH NHS TRUST 4 SY3 8XL
为了确保我使用的是系列而不是数据框,我已将“信任”添加到代码中,如下所示。
map1 = spine['Trust'].map(postcodes_all['Trust'])
KeyError Traceback (most recent call last)
~/anaconda3/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
2896 try:
-> 2897 return self._engine.get_loc(key)
2898 except KeyError:
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: 'Trust'
During handling of the above exception, another exception occurred:
KeyError Traceback (most recent call last)
<ipython-input-68-921448f7c401> in <module>
----> 1 map1 = spine['Trust'].map(postcodes_all['Trust'])
~/anaconda3/lib/python3.7/site-packages/pandas/core/frame.py in __getitem__(self, key)
2993 if self.columns.nlevels > 1:
2994 return self._getitem_multilevel(key)
-> 2995 indexer = self.columns.get_loc(key)
2996 if is_integer(indexer):
2997 indexer = [indexer]
~/anaconda3/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
2897 return self._engine.get_loc(key)
2898 except KeyError:
-> 2899 return self._engine.get_loc(self._maybe_cast_indexer(key))
2900 indexer = self.get_indexer([key], method=method, tolerance=tolerance)
2901 if indexer.ndim > 1 or indexer.size > 1:
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: 'Trust'
我不确定为什么这是不正确的以及关键错误的含义。
最佳答案
您获得所有 NaN 值的原因是因为 spine['Trust']
中没有任何值
可以在 postcodes_all['Trust_title']
的索引中找到。
map()
用于用新值替换旧值。
它需要一个键值对来知道要使用哪个新值
替换每个旧值时。
对于一个系列来说,
它使用索引作为键,使用单个系列列作为值。
关于如何在这种情况下进行调试的提示, 是尝试一个更简单的例子, 例如您链接的 pandas 文档中的一个。 请参阅下面的示例。
import pandas as pd
s = pd.Series(['cat', 'dog', 'rabbit'])
s
## Output
0 cat
1 dog
2 rabbit
dtype: object
<小时/>
s2 = pd.Series(['carnivore', 'omnivore', 'herbivore'])
s2
## Output
0 carnivore
1 omnivore
2 herbivore
dtype: object
<小时/>
s.map(s2)
## Output
0 NaN
1 NaN
2 NaN
dtype: object
返回NaN
,
因为 pandas 无法在 s
中的值之间找到任何匹配的值
以及 s2
中的索引。
将s2
的索引设置为s
的值可以解决这个问题。
# Set the values from `s` as the index in `s2`
s2.index = s
s2
## Output
cat carnivore
dog omnivore
rabbit herbivore
dtype: object
<小时/>
s.map(s2)
## Output
0 carnivore
1 omnivore
2 herbivore
dtype: object
关于python - 将 Series 传递给 Series.map() 时的 NaN 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59245951/