Pandas Dataframe select multiple discontinuous columns/slices

人盡茶涼 提交于 2019-12-10 16:19:45

问题


I have dataframe with >100 columns. I am trying to select the columns 0~32 and #83. It seems that 1 slice works fine with the code below.

df_new = df[df.columns[0:32]]

It does not work with 2 slices code below though. How do I fix the problem?

df_new = df[df.columns[0:32, 83]]

回答1:


Use the np.r_ indexer in conjunction with iloc, like this:

df.iloc[:, np.r_[0:32, 83]]

np.r_[0:32, 83]

array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 83])



回答2:


np.r_ is an excellent answer. The other approach would be to construct it with list and range.

Consider this example:

import pandas as pd

df = pd.DataFrame([range(10)],range(10))
cols = list(range(0,5))+[8]              # 0,1,2,3,4,8
df.iloc[:,cols]



回答3:


I am also interest in this problem. We can choose several separate lines or columns. But it seems slicing operation could be done only once on each axis. Like the following one.

new_df=df.iloc[[2,3,4],[3:4]]

Maybe we can slice first and then concatenate them together.

df1=df.iloc[[2:4],:]
df2=df.iloc[[8:10],:]
new_df=pd.concat([df1,df2],axis=0)


来源:https://stackoverflow.com/questions/50143469/pandas-dataframe-select-multiple-discontinuous-columns-slices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!