Python 根据列的名称和位置选择列

Python 根据列的名称和位置选择列,python,pandas,select,indexing,Python,Pandas,Select,Indexing,例如,我有一个数据框,其中包含以下列: 列名=ID、name、dob、away\u dur、away\u count、in\u dur、in\u time等 我想根据以下条件选择数据并创建新的数据框: 1新数据帧应包括第4列到最后一列和第4列之间的列 2新dataframe不应包含以“in\”开头的列 我知道如何根据名称或位置提取列。但是我能知道如何使用这两个条件来选择数据吗?使用列表理解来筛选列。然后,使用df.tail c = [x for x in df.columns if not x.

例如,我有一个数据框,其中包含以下列: 列名=ID、name、dob、away\u dur、away\u count、in\u dur、in\u time等

我想根据以下条件选择数据并创建新的数据框: 1新数据帧应包括第4列到最后一列和第4列之间的列 2新dataframe不应包含以“in\”开头的列

我知道如何根据名称或位置提取列。但是我能知道如何使用这两个条件来选择数据吗?

使用列表理解来筛选列。然后,使用
df.tail

c = [x for x in df.columns if not x.startswith('in_')]
df = df[c].tail(-3)
使用列表理解来筛选列。然后,使用
df.tail

c = [x for x in df.columns if not x.startswith('in_')]
df = df[c].tail(-3)
你可以用

ndf = df.iloc[:,4:]
ndf = ndf.iloc[:,~ndf.columns.str.startswith('in_')]
你可以用

ndf = df.iloc[:,4:]
ndf = ndf.iloc[:,~ndf.columns.str.startswith('in_')]