Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a value from a cell of a dataframe?

I have constructed a condition that extract exactly one row from my data frame:

d2 = df[(df['l_ext']==l_ext) & (df['item']==item) & (df['wn']==wn) & (df['wd']==1)] 

Now I would like to take a value from a particular column:

val = d2['col_name'] 

But as a result I get a data frame that contains one row and one column (i.e. one cell). It is not what I need. I need one value (one float number). How can I do it in pandas?

like image 827
Roman Avatar asked May 24 '13 07:05

Roman


People also ask

How do I get the value of a column in a data frame?

You can use the loc and iloc functions to access columns in a Pandas DataFrame. Let's see how. If we wanted to access a certain column in our DataFrame, for example the Grades column, we could simply use the loc function and specify the name of the column in order to retrieve it.

How do you select a value from a DataFrame in Python?

Select Data Using Location Index (. This means that you can use dataframe. iloc[0:1, 0:1] to select the cell value at the intersection of the first row and first column of the dataframe. You can expand the range for either the row index or column index to select more data.


1 Answers

If you have a DataFrame with only one row, then access the first (only) row as a Series using iloc, and then the value using the column name:

In [3]: sub_df Out[3]:           A         B 2 -0.133653 -0.030854  In [4]: sub_df.iloc[0] Out[4]: A   -0.133653 B   -0.030854 Name: 2, dtype: float64  In [5]: sub_df.iloc[0]['A'] Out[5]: -0.13365288513107493 
like image 85
Andy Hayden Avatar answered Oct 12 '22 17:10

Andy Hayden