Find the index of items that match the conditions in the pandas data frame / series

Overview

I will introduce how to return the index of the item that matches the condition from the data frame or series of pandas.

Conclusion

df.ix[[df.ix[:,0] == "hoge"],:].index[0]

Commentary

review

df.ix[[df.ix[:,0] == "hoge"],:]

You probably know the syntax, but here I would like to describe how to return a pure index. By the way, the above syntax means "returns the row where column 0 is" hoge "".

Main story

The above result is returned in a pandas dataframe or series (dataframe if you are referencing a dataframe, series if you are referencing a series).

#Data frame
df.ix[[df.ix[:,0] == "hoge"],:] 
#Column with the outermost ix:Since it is specified as multiple columns, the result will be returned in the data frame.

here,

df.ix[[df.ix[:,0] == "hoge"],:].index

If you try, the index of the data frame that matches the condition will be returned as it is. If multiple items meet the conditions, a series with a size larger than 1 will be returned.

Therefore,

df.ix[[df.ix[:,0] == "hoge"],:].index[0]

Then, you can get the index in a format that Python can handle as it is.

Recommended Posts

Find the index of items that match the conditions in the pandas data frame / series
Find out the maximum number of characters in multi-line text stored in a data frame
Comparison of data frame handling in Python (pandas), R, Pig
Changed the default style (CSS) of pandas data frame output by display in Google Colab
Find the sensor installation location that maximizes the amount of acquired data
A collection of Numpy, Pandas Tips that are often used in the field
[Python] Summary of functions that return the index that takes the closest value in the array
Talking about the features that pandas and I were in charge of in the project
How to find the coefficient of the trendline that passes through the vertices in Python
Change the data frame of pandas purchase data (id x product) to a dictionary
Extract periods that match a particular pattern from pandas time series qualitative data
Delete vertices that meet the conditions in networkx
Match the distribution of each group in Python
Find the number of days in a month
Find the divisor of the value entered in python
Find the solution of the nth-order equation in python
The story of reading HSPICE data in Python
Find the sum of unique values with pandas crosstab
I made a program in Python that changes the 1-minute data of FX to an arbitrary time frame (1 hour frame, etc.)
About the inefficiency of data transfer in luigi on-memory
Find the part that is 575 from Wikipedia in Python
Not being aware of the contents of the data in python
Let's use the open data of "Mamebus" in Python
[Memo] Text matching in pandas data frame using flashtext
[Python] Programming to find the number of a in a character string that repeats a specified number of times.
How to calculate the sum or average of time series csv data in an instant
What you should not do in the process of time series data analysis (including reflection)
[Python] Extracts data frames that do not match a specific column with other data frames of Pandas
Try scraping the data of COVID-19 in Tokyo with Python
How to find the optimal number of clusters in k-means
Maya | Find out the number of polygons in the selected object
Code that sets the default value in case of AttributeError
Let's make the analysis of the Titanic sinking data like that
Analyzing data on the number of corona patients in Japan
[Pandas] If the first row data is in the header in DataFrame
Python --Find out number of groups in the regex expression
Find the index of the maximum value (minimum value) of a multidimensional array
Reformat the timeline of the pandas time series plot with matplotlib
[Homology] Count the number of holes in data with Python
Data processing that eliminates the effects of confounding factors (theory)
Graph time series data in Python using pandas and matplotlib
Find the average / standard deviation of the brightness values in the image
How to get an overview of your data in Pandas
Find the eigenvalues of a real symmetric matrix in Python
Get the index of each element of the confusion matrix in Python
Evaluation index that can be specified in GridSearchCV of sklearn
The minimum methods to remember when aggregating data in Pandas