我可能走错了路。。 我正在找大约100家英国医院的邮政编码。我有一份Excel电子表格,上面有英国医院/诊所/etc的总数(14000个)及其地址和邮政编码。你知道吗
我有一个数据帧(脊椎)的手术活动,每年在这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,包括所有14000家医院。你知道吗
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
在英国,医院是信托机构,因此在我的数据框(spine)中,医院名称列=信托机构。我正在尝试将此映射到邮政编码\u all(信任\u标题)中的医院条目。你知道吗
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
它没有找到任何匹配项。医院字段是例如利兹教学医院NHS信托基金会,相同的条目在邮政编码中。你知道吗
有没有办法探究它失败的原因?。我是一名医生,正在努力学习python和pandas来进行数据分析,所以有很多早期步骤。你知道吗
我不确定它是否没有失败,我只是在某个地方定义了错误的数据类型,或者我正在尝试匹配两个本质上不同的列,并且希望能够检查失败的代码。你知道吗
抱歉,在我赶往诊所的时候,手术的内容含糊而简短。你知道吗
更新。你知道吗
根据乔在下面的评论,我把事情简化了。从spine csv中,我将要使用的列定义为“Trust”,在postcode csv中,我将index列定义为“Trust”。你知道吗
我现在肯定是在比较脊椎中的医院标题和邮政编码中的索引字段。我现在在“信任”中得到一个关键错误。你知道吗
我的密码在这里
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()
用于用新值替换旧值。 它需要一个键值对来知道要使用哪个新值 替换每个旧值时。 对于一个系列, 它使用索引作为键,使用单个系列列作为值。你知道吗在这种情况下如何调试的技巧, 用一个简单的例子, e、 g.您链接的熊猫文档中的一个。 请参见下面的示例。你知道吗
NaN
返回, 因为熊猫在s
中的值之间找不到任何匹配的值 以及s2
中的索引。 将s2
的索引设置为s
的值可以解决这个问题。你知道吗相关问题 更多 >
编程相关推荐