[英]Drop multiple columns when column name has space pandas
I have a df我有一个 df
Col 1 Col 2 Col3 Col4
11 10 s 1
12 22 g 10
If I find the column names and Do如果我找到列名并做
<<df.columns
>>Index(['Col 1', 'Col 2' , 'Col3', 'Col4'])
If I want to remove Col 1
and Col 2
from my df , then what to do:如果我想从我的 df 中删除
Col 1
和Col 2
,那么该怎么做:
df.drop(['Col 1, Col 2'], axis=1)
I am getting error:我收到错误:
KeyError: "['Col 1, Col 2'] not found in axis"
EDIT:编辑:
It works with below syntax它适用于以下语法
df.drop(['Col 1', 'Col 2'], axis=1)
If need remove columns with spaces:如果需要删除带有空格的列:
df = df.loc[:, ~df.columns.str.contains('\s')]
print (df)
Col3 Col4
0 s 1
1 g 10
But maybe space is not space, you can test it by ord
, for space got 32
:但也许空间不是空间,你可以通过
ord
测试它,因为空间得到32
:
a = [ord(x) for x in df.columns[0]]
print (a)
[67, 111, 108, 32, 49]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.