python - Getting a scalar by integer location and column label (mixed indexing) -


to scalar @ integer location 0 , column label 'a' in data frame df, chained indexing: df.iloc[0]['a']. works, pandas documentation says chained indexing should avoided.

an alternative come df.iat[0, df.columns.get_loc('a')], typing compared chained indexing. there shorter way this?

note: .ix indexer deprecated.

example:

df=pd.dataframe({'a':[10,20,30,40]}, index=[3,2,1,0])

     ------ 3   10 2   20 1   30 0   40 

the scalar @ integer location 0 in column a 10 , not 40:
df.iat[0, df.columns.get_loc('a')]
otuput: 10

you can refer other post on loc, iloc, at, iat


to answer question directly:
called mixed type indexing. want access 1 dimension position , other label.

to solve problem, need translate either:

  • the position label use loc (or at) label indexing.
  • the label position use iloc (or iat) position indexing.

using loc

we label @ 0 position

df.loc[df.index[0], 'a'] 

or

df.at[df.index[0], 'a'] 

or

df.get_value(df.index[0], 'a') 

using iloc
position of label using pd.index.get_loc

df.iloc[0, df.columns.get_loc('a')] 

or

df.iat[0, df.columns.get_loc('a')] 

or

df.get_value(0, df.columns.get_loc('a'), takable=true) 

i included examples of using pd.dataframe.get_value


Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -