Pandas: Why are double brackets needed to select column after boolean indexing

前端 未结 4 1124
南笙
南笙 2021-01-29 20:08

For a df table like below,

   A B C D
0  0 1 1 1
1  2 3 5 7
3  3 1 2 8

why are the double brackets needed for selecting specific columns after

4条回答
  •  遥遥无期
    2021-01-29 20:53

    For pandas objects (Series, DataFrame), the indexing operator [] only accepts

    1. colname or list of colnames to select column(s)
    2. slicing or Boolean array to select row(s), i.e. it only refers to one dimension of the dataframe.

    For df[[colname(s)]], the interior brackets are for list, and the outside brackets are indexing operator, i.e. you must use double brackets if you select two or more columns. With one column name, single pair of brackets returns a Series, while double brackets return a dataframe.

    Also, df.ix[df['A'] < 3,['A','C']] or df.loc[df['A'] < 3,['A','C']] is better than the chained selection for avoiding returning a copy versus a view of the dataframe.

    Please refer pandas documentation for details

提交回复
热议问题