Returns the value of a single cell from a Pandas DataFrame

I would like to ask a question, which is an extension in this thread:

Select rows from a DataFrame based on the values ​​in a column in pandas .

The code from this thread is shown below:

import pandas as pd import numpy as np df = pd.DataFrame({'A': 'foo bar foo bar foo bar foo foo'.split(), 'B': 'one one two three two two one three'.split(), 'C': np.arange(8), 'D': np.arange(8) * 2}) print(df) # ABCD # 0 foo one 0 0 # 1 bar one 1 2 # 2 foo two 2 4 # 3 bar three 3 6 # 4 foo two 4 8 # 5 bar two 5 10 # 6 foo one 6 12 # 7 foo three 7 14 print(df.loc[df['D'] == 14]) 

This will produce the following result:

  ABCD 7 foo three 7 14 

Based on the code above, how can I return a single β€œvalue”, not a string. That is, how can I return the value '7' or the value 'foo' as opposed to the whole line?

+5
source share
1 answer

@JonahWilliams was close, here's a worker:

 import pandas as pd import numpy as np df = pd.DataFrame({'A': 'foo bar foo bar foo bar foo foo'.split(), 'B': 'one one two three two two one three'.split(), 'C': np.arange(8), 'D': np.arange(8) * 2}) print(df.loc[df['D'] == 14]['A'].index.values) >>>[7] print(df.loc[df['D'] == 14]['A'].values) >>>['foo'] 
+5
source

Source: https://habr.com/ru/post/1233298/


All Articles