Gayatri
Gayatri

Reputation: 3

How to fetch row and column number given a value in dataframe

Image of CSV

I have a blob (CSV) in a database. I prepared a string buffer and created a panda data frame. The CSV file does not have column names for certain columns and certain column names are repeated.

For Example: In case of needing to fetch the intersecting value for B5 = search_row and E2 = search_column. ie E5 = value_to_be_fetched.

I just have the text value search_row and search_column. How do I find the row index as B5 and column index as E2? As well as fetch the value E5 = value_to_be_fetched.

Upvotes: 0

Views: 1070

Answers (1)

jezrael
jezrael

Reputation: 862471

If values search_row and search_column are unique in all data use np.where for positions and select by DataFrame.iloc:

df = pd.DataFrame({'A':list('abcdef'),
                   'B':[4,5,4,5,500,4],
                   'C':[7,8,9,4,2,3],
                   'D':[1,300,5,7,1,0],
                   'E':[5,3,6,9,2,4],
                   'F':list('aaabbb')}, index = [1] * 6)
df.columns = ['A'] * 6
print (df)
   A    A  A    A  A  A
1  a    4  7    1  5  a
1  b    5  8  300  3  a
1  c    4  9    5  6  a
1  d    5  4    7  9  b
1  e  500  2    1  2  b
1  f    4  3    0  4  b

a = np.where(df == 500)[0]
b = np.where(df == 300)[1]
print (a)
[4]
print (b)
[3]

c = df.iloc[a[0],b[0]]
print (c)
1

But if values should be duplicated is possible select only first occurence, because np.where return array(s) with length > 1:

a = np.where(df == 5)[0]
b = np.where(df == 2)[1]
print (a)
[0 1 2 3]
print (b)
[2 4]

c = df.iloc[a[0],b[0]]
print (c)
7

a = np.where(df == 2)[0]
b = np.where(df == 5)[1]
print (a)
[4 4]
print (b)
[4 1 3 1]

c = df.iloc[a[0],b[0]]
print (c)
2

Upvotes: 2

Related Questions