2018年5月2日 星期三

[ Python 常見問題 ] Pandas - Get first row value of a given column

Source From Here 
Question 
This seems like a ridiculously easy question... but I'm not seeing the easy answer I was expecting. So, how do I get the value at an nth row of a given column in Pandas? (I am particularly interested in the first row, but would be interested in a more general practice as well). 

How-To 
To select the ith row, use pandas.DataFrame.iloc/pandas.Series.iloc: 
>>> import pandas as pd 
>>> import numpy as np 
>>> df = pd.DataFrame(np.array([['john', 37], ['peter', 28]]), columns=["name", "age"]) 
>>> df.iloc[1] 
name peter 
age 28 
Name: 1, dtype: object

To select the ith value in the 'name' column you could use: 
>>> df['name'].iloc[1] 
'peter' 
>>> df['name'].iloc[0] 
'john'


沒有留言:

張貼留言

[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...