python - A straightforword method to select columns by position -


i trying clarify how can manage pandas methods call columns , rows in dataframe. example clarify issue

dic = {'a': [1, 5, 2, 7], 'b': [6, 8, 4, 2], 'c': [5, 3, 2, 7]} df = pd.dataframe(dic, index = ['e', 'f', 'g', 'h'] ) 

than

df =      b  c e  1  6  5 f  5  8  3 g  2  4  2 h  7  2  7 

now if want select column 'a' have type

df['a'] 

while if want select row 'e' have use ".loc" method

df.loc['e'] 

if don't know name of row, it's position ( 0 in case) can use "iloc" method

df.iloc[0] 

what looks missing method calling columns position , not name, "equivalent columns of 'iloc' method rows". way can find is

df[df.keys()[0]] 

is there like

df.iloccolumn[0] 

?

you can add : because first argument position of selected indexes , second position of columns in function iloc:

and : means indexes in dataframe:

print (df.iloc[:,0]) e    1 f    5 g    2 h    7 name: a, dtype: int64 

if need select first index , first column value:

print (df.iloc[0,0]) 1 

solution ix work nice if need select index name , column position:

print (df.ix['e',0]) 1 

Comments