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


沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...