python - 使用来自另一个 DataFrame 的值进行索引
问题描述
熊猫版本 1.1.3。
我正在使用 pandas 中的两个数据框(表单 csv),一个包含我正在分析的数据,另一个包含标签。两者都包含一个带有标识号的列。我已将数据框 2 的行索引设置为标识号。我正在使用一个 nlp 处理库来分析数据帧 1 中的数据,它返回一个布尔值。我想在数据帧 2 中索引一个值,在一个花哨的索引方法中使用数据帧 1 中的标识号。
df1 看起来像这样:
ID report
1 A1 'bla bla'
2 A1 'blo blo'
3 A1 'blo bla'
4 A2 'bla bla'
5 A2 'blo blo'
6 A3 'foo blo'
df2 看起来像这样:
label hypothesis
ID
A1 0 0
A2 1 0
A3 1 0
A4 0 0
A5 1 0
A6 0 0
这是我的代码:
for i, row in df1.iterrows():
index = row['ID']
display(index)
report = row['report']
doc = nlp(report)
if boole_is_True(doc):
df2[[index, 'hypothesis']] = 1
这是结果:
'A1'
'A1'
'A1'
'A2'
bla bla
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-22-f9ba77c1e7ea> in <module>
17 print(report)
---> 18 df2[[index, 'hypothesis']] = 1
~\anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
2906 if is_iterator(key):
2907 key = list(key)
-> 2908 indexer = self.loc._get_listlike_indexer(key, axis=1, raise_missing=True)[1]
2909
2910 # take() does not accept boolean indexers
~\anaconda3\lib\site-packages\pandas\core\indexing.py in _get_listlike_indexer(self, key, axis, raise_missing)
1252 keyarr, indexer, new_indexer = ax._reindex_non_unique(keyarr)
1253
-> 1254 self._validate_read_indexer(keyarr, indexer, axis, raise_missing=raise_missing)
1255 return keyarr, indexer
1256
~\anaconda3\lib\site-packages\pandas\core\indexing.py in _validate_read_indexer(self, key, indexer, axis, raise_missing)
1302 if raise_missing:
1303 not_found = list(set(key) - set(ax))
-> 1304 raise KeyError(f"{not_found} not in index")
1305
1306 # we skip the warning on Categorical
KeyError: "['A2'] not in index"
如何去掉方括号?为什么会有括号?这个问题有什么更好的标题,以便其他人更容易找到?
提前致谢!我的实际数据包含医疗报告,所以我害怕我无法向您展示我的实际数据和代码。
解决方案
当您使用:
df2[[index, 'hypothesis']] = 1
pandas 在列索引中搜索传递的index
值,但无法A2
在您的列名中找到。在您的情况下,您试图在 的行索引中找到这些值df2
,因此您需要编写如下:
df2.loc[index, 'hypothesis'] = 1
.loc[]
接受行和列索引值。
推荐阅读
- typescript - 如何提取函数第一个参数的类型,但同时保持泛型?
- macos - 无法在 MacM1 上安装 tensorflow-macos(安装 grpcio 时出错)
- html - 为 redshift 中的查询创建超链接
- autodesk-forge - 我们可以在 Autodesk Forge 查看器中实现 Autodesk 数量起飞功能吗?
- javascript - Postgresql 日期时间分辨率毫秒兼容 Javascript 日期
- reactjs - 如何优化状态变化引起的react re-renders?
- nginx - 版本“networking.k8s.io/v1beta1”中类型“IngressClass”没有匹配项
- javascript - 加载背景图像后渲染元素
- javascript - 我怎样才能改变我作为听众的形象?
- php - 使用 PHP 打开日志文件并在该文件中查找 IP 地址