python - 根据索引在 Pandas 数据框中查找特定列的值

标签 python pandas

<分区>

我正在使用 Python - 3.6 和 pandas - 0.24.1

我有一个 Pandas 数据框df1:

    col1    col2
0  8388611  3.9386
1  8388612  1.9386

我需要在特定索引上找到 col1 的值

 print(df1['col1'][1])

错误:

Traceback (most recent call last):
  File "/home/runner/.site-packages/pandas/core/indexes/base.py", line 2656, inget_loc
    return self._engine.get_loc(key)
  File "pandas/_libs/index.pyx", line 108, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 1601, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 1608, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 1

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "main.py", line 24, in <module>
    print(df1['col1'][1])
  File "/home/runner/.site-packages/pandas/core/frame.py", line 2927, in __getitem__
    indexer = self.columns.get_loc(key)
  File "/home/runner/.site-packages/pandas/core/indexes/multi.py", line 2397, in get_loc
    loc = self._get_level_indexer(key, level=0)
  File "/home/runner/.site-packages/pandas/core/indexes/multi.py", line 2652, in _get_level_indexer
    code = level_index.get_loc(key)
  File "/home/runner/.site-packages/pandas/core/indexes/base.py", line 2658, inget_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))
  File "pandas/_libs/index.pyx", line 108, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 1601, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 1608, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 1

在 pandas 中迭代特定列的所有元素的理想方法是什么?

最佳答案

你可以使用 iloc

df["列"].iloc[索引]

关于python - 根据索引在 Pandas 数据框中查找特定列的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55157530/

相关文章:

python - Tensorflow:如何将 1 阶张量转换为 2 阶张量

python - Plotly:如何更改散点图散点图的配色方案?

python - pandas 与 scipy 中的 skew 和 kurtosis 函数有什么区别?

python - 如何使用日志记录将日志发送到松弛作为 python 和 flask 中的消息?

python - Sprite 在一定距离限制内遵循另一种灵活的方式

python - 如何使用map函数将数据框中的所有负数转换为python pandas中所有数字的平均值?

python - 如何从对象为 datetime.time 类型的 Pandas DataFrame.Index 中添加/减去时间(小时、分钟等)?

python - 对 pandas 中的列表列表中的行进行分组

python - for循环pandas和numpy : Performance

python - 如何根据日期将多索引 df 分成 80/20 部分?