roudan
roudan

Reputation: 4208

How to return a cell value from Pandas instead of a series?

I am struggling a lot using pandas LOC to get one cell value. It always return a series.

For example:

    filter=(data['Well']==well)&(data['Date']==thisdate)
    data_thiswell_thisdate=data.loc[filter,'Steam']   # after filter, it is a series
    print(data_thiswell_thisdate)

When printing, the result is below with index and value:

4302    0.0000

In order to get value, I have to use values[0]. Why? Is it LOC to get cell value?

data_thiswell_thisdate=data.loc[filter,'Steam'].values[0]

Could you please help me understand how to use LOC in pandas? Thanks.

Upvotes: 2

Views: 3536

Answers (1)

MarianD
MarianD

Reputation: 14141

in order to get value, I have to do using values[0]. why? Is it LOC to get cell value?

You obtained a series, although only 1-element series.
It is something like a 1-element list [0.0000] — to obtain its (only one) element, you must nevertheless use its index ([0]).

No, it is not the .loc[] method, it is common indexing starting with index 0.

.loc[] method uses a row label (i.e. an index value):

data_thiswell_thisdate.loc[4302]

If you want to use .loc[] to obtain an element from a dataframe (as opposed to a series), you provide a row label and a column label (in this order) — for example in your case you may directly access your value in the form

data.loc[4302, 'Steam']

(of course, if you knew the index value (4302) in advance).

Upvotes: 1

Related Questions