我正在尝试从DataFrame对象中选择多个值,如下所示:
Int64Index: 100 entries, 0 to 99
Data columns (total 6 columns):
Cell 68 non-null values
Sequence Protocol 68 non-null values
dtypes: float64(2), object(4)
我想根据Cell列中的值选择多个值。我知道如何通过对数据运行for循环来做到这一点,但我觉得很笨重。
理想情况下,我想传递一个“单元格”列表,它会给我另一个DataFrame对象。
示例输出如下:
idx = cell_info.Cell == ['1','3','5']
为idx
提供以下内容:
0 True
1 False
2 True
3 False
4 True
5 False
6 False
...
np.sum(idx)
等于3
我尝试过Cell重建索引:
cell_info.reindex(index=cell_info.Cell)
导致空DataFrame。
我尝试传递以下内容来获取索引,其中data.keys()是我想要索引的单元格列表:
idx = cell_info.Cell == data.keys()
导致所有值为False,np.sum(idx)
将产生0
(但如果我自己调用它们,则不会产生np.where(cell_info.Cell == data.keys())
。
如果我执行以下操作:
np.where(cell_info.Cell == list(data.keys()))
如果我执行以下操作,它将返回空数组:
ValueError: Arrays were different lengths: 100 vs 19
它出错了:
{{1}}
如果您能解释将列表传递给索引的合适方式以及为什么最后两个调用存在差异,那将是很好的。