2017年7月15日 星期六

[ Python 常見問題 ] Pandas - How to get a value from a cell of a data frame?

Source From Here 
Question 
As title. How do I access one specific cell of DataFrame? 

How-To 
Try pandas.DataFrame.iloc (Purely integer-location based indexing for selection by position.): 
>>> import pandas as pd 
>>> d1 = {'Name': 'John', 'Age': 36} 
>>> d2 = {'Name': 'Mary', 'Age': 20} 
>>> dlist = [d1, d2] 
>>> df = pd.DataFrame(dlist) // Translate list with dict object into DataFrame object 
>>> df // Show content of DataFrame 
Age Name 
0 36 John 
1 20 Mary
 
>>> df.iloc[0]['Name'] // Access the first row of DataFrame and then retrieve the 'Name' of it 
'John' 
>>> df.iloc[1]['Age'] // Access the second row of DataFrame and then retrieve the 'Age' of it 
20


沒有留言:

張貼留言

[ Python 文章收集 ] SQLAlchemy quick start with PostgreSQL

Source From   Here   Preface   This is a quick tutorial for getting started with   SQLAlchemy Core API .   Prerequisites   In this quick st...