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).
For example, let's say I want to pull the 1.2 value in Btime as a variable.
Whats the right way to do this?
df_test =
ATime X Y Z Btime C D E
0 1.2 2 15 2 1.2 12 25 12
1 1.4 3 12 1 1.3 13 22 11
2 1.5 1 10 6 1.4 11 20 16
3 1.6 2 9 10 1.7 12 29 12
4 1.9 1 1 9 1.9 11 21 19
5 2.0 0 0 0 2.0 8 10 11
6 2.4 0 0 0 2.4 10 12 15
Another way to do this:
This way seems to be faster than using
.iloc
:To select the
ith
row, useiloc
:To select the ith value in the
Btime
column you could use:Warning: I had previously suggested
df_test.ix[i, 'Btime']
. But this is not guaranteed to give you theith
value sinceix
tries to index by label before trying to index by position. So if the DataFrame has an integer index which is not in sorted order starting at 0, then usingix[i]
will return the row labeledi
rather than theith
row. For example,In a general way, if you want to pick up the first N rows from the J column from pandas dataframe the best way to do this is:
data = dataframe[0:N][:,J]
Note that the answer from @unutbu will be correct until you want to set the value to something new, then it will not work if your dataframe is a view.
Another approach that will consistently work with both setting and getting is:
df.iloc[0].head(1)
- First data set only from entire first row.df.iloc[0]
- Entire First row in column.