Slicing multiple ranges of columns in Pandas, by list of names

前端 未结 2 723
悲哀的现实
悲哀的现实 2020-12-03 18:29

I am trying to select multiple columns in a Pandas dataframe in two different approaches:

1)via the columns number, for examples, columns 1-3 and columns 6 onwards.<

2条回答
  •  执念已碎
    2020-12-03 19:06

    I’m not sure what exactly you are asking but in general DataFrame.loc allows you to select by label, DataFrame.iloc by index.

    For example selecting columns # 0, 1 and 4:

    dataframe.iloc[:, [0, 1, 4]]
    

    and selecting columns labelled 'A', 'B' and 'C':

    dataframe.loc[:, ['A', 'B', 'C']]
    

提交回复
热议问题