python dataframe pandas使用int删除列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20297317/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
python dataframe pandas drop column using int
提问by user1802143
I understand that to drop a column you use df.drop('column name', axis=1). Is there a way to drop a column using a numerical index instead of the column name?
我知道要删除列,请使用 df.drop('column name', axis=1)。有没有办法使用数字索引而不是列名来删除列?
回答by mkln
if you really want to do it with integers (but why?), then you could build a dictionary.
如果你真的想用整数来做(但为什么?),那么你可以建立一个字典。
col_dict = {x: col for x, col in enumerate(df.columns)}
then df = df.drop(col_dict[0], 1)will work as desired
然后df = df.drop(col_dict[0], 1)将按需要工作
edit: you can put it in a function that does that for you, though this way it creates the dictionary every time you call it
编辑:你可以把它放在一个为你做这件事的函数中,尽管这样它每次调用它时都会创建字典
def drop_col_n(df, col_n_to_drop):
col_dict = {x: col for x, col in enumerate(df.columns)}
return df.drop(col_dict[col_n_to_drop], 1)
df = drop_col_n(df, 2)
回答by Roman Pekar
You can delete column on iindex like this:
您可以i像这样删除索引上的列:
df.drop(df.columns[i], axis=1)
It could work strange, if you have duplicate names in columns, so to do this you can rename column you want to delete column by new name. Or you can reassign DataFrame like this:
如果列中有重复的名称,这可能会很奇怪,因此为此您可以重命名要按新名称删除列的列。或者您可以像这样重新分配 DataFrame:
df = df.iloc[:, [j for j, c in enumerate(df.columns) if j != i]]
回答by muon
Drop multiple columns like this:
像这样删除多个列:
cols = [1,2,4,5,12]
df.drop(df.columns[cols],axis=1,inplace=True)
inplace=Trueis used to make the changes in the dataframe itself without doing the column dropping on a copy of the data frame. If you need to keep your original intact, use:
inplace=True用于在数据帧本身中进行更改,而无需将列放在数据帧的副本上。如果您需要保持原件完好无损,请使用:
df_after_dropping = df.drop(df.columns[cols],axis=1)
回答by Saeed
If there are multiple columns with identical names, the solutions given here so far will remove all of the columns, which may not be what one is looking for. This may be the case if one is trying to remove duplicate columns except one instance. The example below clarifies this situation:
如果有多个具有相同名称的列,那么到目前为止给出的解决方案将删除所有列,这可能不是人们正在寻找的。如果试图删除除一个实例之外的重复列,则可能会出现这种情况。下面的例子阐明了这种情况:
# make a df with duplicate columns 'x'
df = pd.DataFrame({'x': range(5) , 'x':range(5), 'y':range(6, 11)}, columns = ['x', 'x', 'y'])
df
Out[495]:
x x y
0 0 0 6
1 1 1 7
2 2 2 8
3 3 3 9
4 4 4 10
# attempting to drop the first column according to the solution offered so far
df.drop(df.columns[0], axis = 1)
y
0 6
1 7
2 8
3 9
4 10
As you can see, both Xs columns were dropped. Alternative solution:
如您所见,两个 Xs 列都被删除了。替代解决方案:
column_numbers = [x for x in range(df.shape[1])] # list of columns' integer indices
column_numbers .remove(0) #removing column integer index 0
df.iloc[:, column_numbers] #return all columns except the 0th column
x y
0 0 6
1 1 7
2 2 8
3 3 9
4 4 10
As you can see, this truly removed only the 0th column (first 'x').
如您所见,这确实仅删除了第 0 列(第一个“x”)。
回答by Thunder
Since there can be multiple columns with same name , we should first rename the columns. Here is code for the solution.
由于可以有多个具有相同名称的列,我们应该首先重命名这些列。这是解决方案的代码。
df.columns=list(range(0,len(df.columns)))
df.drop(columns=[1,2])#drop second and third columns
回答by Mojtaba Peyrovi
回答by Kripalu Sar
You need to identify the columns based on their position in dataframe. For example, if you want to drop (del) column number 2,3 and 5, it will be,
您需要根据它们在数据框中的位置来识别列。例如,如果要删除 (del) 列号 2,3 和 5,它将是,
df.drop(df.columns[[2,3,5]], axis = 1)
回答by Cam
If you have two columns with the same name. One simple way is to manually rename the columns like this:-
如果您有两个名称相同的列。一种简单的方法是像这样手动重命名列:-
df.columns = ['column1', 'column2', 'column3']
Then you can drop via column index as you requested, like this:-
然后您可以根据要求通过列索引删除,如下所示:-
df.drop(df.columns[1], axis=1, inplace=True)
df.column[1]will drop index 1.
df.column[1]将删除索引 1。
Remember axis 1 = columns and axis 0 = rows.
记住轴 1 = 列和轴 0 = 行。

