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'


沒有留言:

張貼留言

[ Py DS ] Ch1 - IPython: Beyond Normal Python

Source From  Here   Keyboard Shortcuts in the IPython Shell   If you spend any amount of time on the computer, you’ve probably found a u...