fastest way to locate specific cell in pandas dataframe?

Felix Burkhardt

I wondered how to easy access the column index for a specific cell in pandas.

E.g. if i got this dataframe:

df = pd.DataFrame.from_dict({0:['01_CF56_1'],   1:['05_CF41_3'], 2:['06_CF44_2']})

i'd like to have a function that gives me

f('05_CF41_3') => 1

because this value is in the column index 1 (I don't care about the row)

of course the naive way would be to go through all rows and columns and match the cell contents, but i wonder if there is a more elegant solution

Stef

You could use the second element of indices to get the column indices and use it in where. Then find the first valid column index using argmax:

np.argmax(np.where(df == '05_CF41_3', np.indices(df.shape)[1], -1) >= 0)

returns 1.

If the searched for value may occur in multiple columns you could use something like that to get a list of all column numbers where this value occurs:

a = np.where(df == '05_CF41_3', np.indices(df.shape)[1], -1)
list(set(a[a>=0]))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Fastest Way To Filter A Pandas Dataframe Using A List

Fastest Way to Drop Duplicated Index in a Pandas DataFrame

Fastest way to sort each row in a pandas dataframe

Add a tuple to a specific cell of a pandas dataframe

What's the fastest way to acces a Pandas DataFrame?

Fastest way to split a pandas dataframe into a list of subdataframes

What is the fastest way to check a pandas dataframe for elements?

Fastest way to filter out pandas dataframe rows containing special characters

Assign a numpy array to a specific cell of a pandas dataframe

fastest way to create pandas dataframe rows for combination of values from lists

Fastest method of finding and replacing row-specific data in a pandas DataFrame

Fastest way to iterate subsets of rows in pandas dataframe based on condition

Fastest way to assign value to pandas cell

Fastest way to add rows to existing pandas dataframe

Fastest way to sample Pandas Dataframe?

Fastest way to iterate function over pandas dataframe

Fastest way to "unpack' a pandas dataframe

Fastest way to locate a line which contains a specific word in a large text file

Fastest way to filter a pandas dataframe on multiple columns

For a specific cell in a pandas dataframe, remove an element of a list

Pandas: What is the fastest way to search a large dataframe

Fastest way to check which dates exist in another pandas dataframe

Highlight a specific cell in a pandas dataframe

fastest way to apply an async function to pandas dataframe

fastest way to access dataframe cell by colums values?

How to locate the Occurrences of a Specific value in pandas Dataframe

Fastest way to join coulmn values in pandas dataframe?

Fastest way to turn large pandas dataframe in a dictionary with a specific structure

Select a specific cell in Pandas dataframe