The Python Oracle

How can I get a value from a cell of a dataframe?

Python syntax, remembered forever. Memorabit: https://memorab.it

This video is brought to you by Memorabit, an app that sends words, definitions and translations as reminders at unexpected moments, so they stay in memory. Built on cognitive science principles, without AI. Start free on web, iOS and Android:
iOS: https://apps.apple.com/it/app/memorabit/...
Android: https://play.google.com/store/apps/detai...
--------------------------------------------------
Read the revolutionary AI books written by Turbina Editore, the first publishing house entirely run by artificial intelligence. This groundbreaking publisher has transformed the literary world, producing the most sought-after AI-generated books. Start reading today at:
https://turbinaeditore.com/en/
--------------------------------------------------

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Take control of your privacy with Proton's trusted, Swiss-based, secure services.
Choose what you need and safeguard your digital life:
Mail: https://go.getproton.me/SH1CU
VPN: https://go.getproton.me/SH1DI
Password Manager: https://go.getproton.me/SH1DJ
Drive: https://go.getproton.me/SH1CT


Music by Eric Matyas
https://www.soundimage.org
Track title: Sunrise at the Stream

--

Chapters
00:00 How Can I Get A Value From A Cell Of A Dataframe?
00:34 Accepted Answer Score 840
00:54 Answer 2 Score 389
01:12 Answer 3 Score 54
01:34 Answer 4 Score 346
01:46 Thank you

--

Full question
https://stackoverflow.com/questions/1672...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #pandas #dataframe

#avk47



ACCEPTED ANSWER

Score 840


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



ANSWER 2

Score 389


These are fast access methods for scalars:

In [15]: df = pandas.DataFrame(numpy.random.randn(5, 3), columns=list('ABC'))

In [16]: df
Out[16]:
          A         B         C
0 -0.074172 -0.090626  0.038272
1 -0.128545  0.762088 -0.714816
2  0.201498 -0.734963  0.558397
3  1.563307 -1.186415  0.848246
4  0.205171  0.962514  0.037709

In [17]: df.iat[0, 0]
Out[17]: -0.074171888537611502

In [18]: df.at[0, 'A']
Out[18]: -0.074171888537611502



ANSWER 3

Score 346


You can turn your 1x1 dataframe into a NumPy array, then access the first and only value of that array:

val = d2['col_name'].values[0]



ANSWER 4

Score 54


Most answers are using iloc which is good for selection by position.

If you need selection-by-label, loc would be more convenient.

For getting a value explicitly (equiv to deprecated df.get_value('a','A'))

# This is also equivalent to df1.at['a','A']
In [55]: df1.loc['a', 'A']
Out[55]: 0.13200317033032932