Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I retrieve a row by index value from a Pandas DataFrame?

Tags:

I have created a dataframe and set an index:

df = pd.DataFrame(np.random.randn(8, 4),columns=['A', 'B', 'C', 'D']) df = df.set_index('A') 

The dataframe looks like this:

                  B         C         D A  0.687263 -1.700568  0.140175  1.420394 -0.212621 -0.700442 -0.041497 -1.034021 -0.614214 -0.437313 -0.464493 -1.182492 -0.885062  0.203892 -0.412400 -0.578346 -1.222661  2.014908 -0.463674 -0.378910  0.132472 -0.389512  0.623531 -0.788556 -1.083620  1.167158 -0.558217 -0.222078  1.066270 -0.215586 -0.884757 -0.878557 

How do I get the value of B in the row for which A is 0.687263?

I've tried:

e = df.loc(0.687263) 

This gives me a LocIndexer object, rather than the row I'd expect (also I'd like to specify that it should be a single row if possible):

<pandas.core.indexing._LocIndexer object at 0x10385e210> 

And if I now try e['B'] I get an error.

How do I get the value of B?

like image 278
Richard Avatar asked Mar 24 '16 13:03

Richard


People also ask

How do I get a row of pandas DataFrame by index?

Often you may want to select the rows of a pandas DataFrame based on their index value. If you'd like to select rows based on integer indexing, you can use the . iloc function.

How do you select rows based on index list?

Alternatively, you can select rows from the list index by using df. loc[df. index[]] method. loc[] method is used to select the rows by labels.

How can I get specific row number in pandas?

To get the nth row in a Pandas DataFrame, we can use the iloc() method. For example, df. iloc[4] will return the 5th row because row numbers start from 0.


1 Answers

pandas rounds values when it prints a dataframe. The actual value you are trying to index on is:

1.764052345967664

import pandas as pd import numpy as np  np.random.seed(0) df = pd.DataFrame(np.random.randn(8, 4),columns=['A', 'B', 'C', 'D'])  df = df.set_index('A') print df                     B         C         D A                                        1.764052  0.400157  0.978738  2.240893  1.867558 -0.977278  0.950088 -0.151357 -0.103219  0.410599  0.144044  1.454274  0.761038  0.121675  0.443863  0.333674  1.494079 -0.205158  0.313068 -0.854096 -2.552990  0.653619  0.864436 -0.742165  2.269755 -1.454366  0.045759 -0.187184  1.532779  1.469359  0.154947  0.378163   df.loc[1.764052345967664] Out[32]:  B    0.400157 C    0.978738 D    2.240893 Name: 1.76405234597, dtype: float64 
like image 110
sorpha Avatar answered Oct 10 '22 17:10

sorpha