Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: Get cell value by row index and column name [duplicate]

Tags:

python

pandas

Let's say we have a pandas dataframe:

   name  age  sal
0  Alex   20  100
1  Jane   15  200
2  John   25  300
3   Lsd   23  392
4   Mari  21  380

Let's say, a few rows are now deleted and we don't know the indexes that have been deleted. For example, we delete row index 1 using df.drop([1]). And now the data frame comes down to this:

  fname  age  sal
0  Alex   20  100
2  John   25  300
3   Lsd   23  392
4   Mari  21  380

I would like to get the value from row index 3 and column "age". It should return 23. How do I do that?

df.iloc[3, df.columns.get_loc('age')] does not work because it will return 21. I guess iloc takes the consecutive row index?

like image 682
Sedmaister Avatar asked Dec 28 '25 16:12

Sedmaister


1 Answers

Use .loc to get rows by label and .iloc to get rows by position:

>>> df.loc[3, 'age']
23

>>> df.iloc[2, df.columns.get_loc('age')]
23

More about Indexing and selecting data

like image 138
Corralien Avatar answered Dec 30 '25 06:12

Corralien



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!