简体   繁体   English

如何在 pandas.DataFrame 中找到值的位置?

[英]How do I find the position of a value in a pandas.DataFrame?

I want to search for 'row3' in the index of the DataFrame below, which should be 2 for a zero-based array.我想在下面的 DataFrame 的索引中搜索“row3”,对于从零开始的数组,它应该是 2。

import numpy as np
import pandas as pd

rownames = ['row1', 'row2', 'row3', 'row4', 'row5']
colnames = ['col1', 'col2', 'col3', 'col4']

# Create a 5 row by 4 column array of integers from 0 to 19
integers = np.arange(20).reshape((5, 4))
table = pd.DataFrame(integers, index=rownames, columns=colnames)

Is there a function which return the row number of 'row3'?是否有返回“row3”行号的函数? Thanks in advance for any help.在此先感谢您的帮助。

You could use Index.get_loc ( docs ):您可以使用Index.get_loc ( docs ):

>>> table.index.get_loc("row3")
2
>>> table.iloc[table.index.get_loc("row3")]
col1     8
col2     9
col3    10
col4    11
Name: row3, dtype: int64
>>> table.loc["row3"]
col1     8
col2     9
col3    10
col4    11
Name: row3, dtype: int64

But this is a somewhat unusual access pattern-- never need it myself.但这是一种有点不寻常的访问模式——我自己从不需要它。

You may just find the length of df from 'row1' till 'row3' and decrease it by 1 (if you are starting count from 0).您可能只是找到从“row1”到“row3”的 df 长度并将其减少 1(如果您从 0 开始计数)。

s = 'row3'
print(s,'number is', len(table['row1':s]) - 1)

returns返回

row3 number is 2

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 pandas.DataFrame:如何在同一 pandas.ZBA834BA059A19A3789EZ57C 中合并具有公共列值的行 - pandas.DataFrame: How to merge rows with a common column value in the same pandas.DataFrame Pandas.DataFrame - 查找值可用的最早日期 - Pandas.DataFrame - find the oldest date for which a value is available 查找pandas.DataFrame中下一个值组的行号 - Find row number of the next value group in a pandas.DataFrame 如何在 pandas.dataframe 中找到高于平均值的行数? - how to find numbers of row above mean in pandas.dataframe? 如何在 pandas.DataFrame 上将“.”替换为“,” - How To Replace " . " With " , " on a pandas.DataFrame 如何遍历 Pandas.DataFrame 和 append 中的一列 ZC1C425Z078E683894FC1AB4 的结果到同一行 17? - How do I iterate over a column in a Pandas.DataFrame and append the result of a function to the same row? Python:如何将 > 1 个变量的列表显示为 pandas.DataFrame 中的单独列? - Python: How do I show list with > 1 variable as separate columns in pandas.DataFrame? 如何使用对齐的空格chacarcters将python pandas.DataFrame写入文件? - How do I write a python pandas.DataFrame to a file using aligned space chacarcters? 如何在 ZE9713AE04A02A810D794ZZIoTDB 中使用 python 的 pandas.dataframe 功能 - How do I use pandas.dataframe functions of python in Apache IoTDB 如何将 python pandas.Dataframe 对象作为参数传递给 celery 任务? - How do I pass in a python pandas.Dataframe object as an argument to a celery task?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM