Find the Index of the Column in Data Frame That Contains the String as Value

Find the index of the column in data frame that contains the string as value

Something like this?

 which(apply(df, 2, function(x) any(grepl("a", x))))

The steps are:

  1. With apply go over each column
  2. Search if a is in this column with grepl
  3. Since we get a vector back, use any to get TRUE if any element has been matched to a
  4. Finally check which elements (columns) are TRUE (i.e. contain the searched letter a).

Finding index and column number of strings in dataframe

You can use np.argwhere:

print(np.argwhere(df.values == "apple"))

Prints:

[[0 1]
[2 1]]

As a list:

print(np.argwhere(df.values == "apple").tolist())

Prints:

[[0, 1], [2, 1]]

Get index of column containing string of key:value pairs

Your question is still a bit unclear. Does this solve your problem:

If the Student_info column contains strings instead of dictionaries then do this first:

df.Student_info = df.Student_info.map(eval)

Now you can use .str.get to extract the indices for the rows that meet the requirement:

indices = df[df.Student_info.str.get('FirstName').eq('')].index

Result:

Int64Index([2, 3], dtype='int64')

So

df.loc[indices]

results in:

   Name                                       Student_info School
2 Ram {'FirstName': '', 'LastName': 'Ram', 'birthDat...' ABC
3 John {'FirstName': '', 'LastName': 'Mac', 'birthDat...' ABC

Find the index of a string value in a pandas DataFrame

One way with underlying array data -

df.columns[(df.values=='foo').any(0)].tolist()

Sample run -

In [209]: df
Out[209]:
A B C D
0 10 foo 3 some
1 20 bar 4 foo
2 42 blah 5 thing

In [210]: df.columns[(df.values=='foo').any(0)].tolist()
Out[210]: ['B', 'D']

If you are looking for just the column-mask -

In [205]: (df.values=='foo').any(0)
Out[205]: array([False, True, False, True], dtype=bool)

Python Pandas Select Index Value Referencing String Value in a Column

you can combine iloc and loc into one statement:

original_df.iloc[original_df.loc[original_df['newinfo'] == 'x'].index-1]

the loc statement is taking the index of where the condition (where newinfo is x) and then getting the index of that value. iloc then takes those indexes and givies you the result you are looking for

judging from your quesiton, you may need a list of these values in the futre. try df1.iloc[df1.loc[df1['newinfo'] == 'x'].index-1].index.tolist()

edit to get the desired output:

original_df.iloc[original_df.loc[original_df['newinfo'] == 'x'].index[-1]-1]

# added a [0] at the end below to get just the value of `4`
original_df.iloc[original_df.loc[original_df['newinfo'] == 'x'].index[-1]-1][0]

Find the index of the row in data frame that contain one element in a string vector

If you have a large data frame and you wish to check all columns, try this

x <- c("a", "k", "n")

Reduce(union, lapply(x, function(a) which(rowSums(df == a) > 0)))
# [1] 1 5 2

and of course you can sort the end result.

Python get index of columns with matching values

This may not be exactly what you are looking for but I would not use a filter query. I would do this since you already know the index position of the row of interest is 2.

import pandas as pd
import numpy as np

df = pd.DataFrame({0: {0: 'March', 1: 2019, 2: 'A'},
1: {0: 'April', 1: 2019, 2: 'E'},
2: {0: 'May', 1: 2019, 2: 'F'}})

indices = np.where(df.iloc[2,:].isin(['A', 'E']))

This will yield a tuple of {0, 1}



Related Topics



Leave a reply



Submit