删除 pandas 中的单个列和范围列 [英] Removing single and range of columns in pandas
本文介绍了删除 pandas 中的单个列和范围列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
删除列[3]
和列[9:15]
的最快方法是什么?
(我只能使用 df.drop
方法分两步删除列)
What is the fastest way to drop columns[3]
and columns[9:15]
?
(I'm only able to remove the columns in 2 steps using the df.drop
method)
1 2 3 4 5 6 .. n
A x x x x x x .. x
B x x x x x x .. x
C x x x x x x .. x
推荐答案
实际上,您可以使用 pd.DataFrame.drop
。您可以使用 np.r _
组合多个索引和范围。这是一个演示:
You can, in fact, use pd.DataFrame.drop
in one step. You can use np.r_
to combine multiple indices and ranges. Here's a demo:
df = pd.DataFrame(np.random.random((3, 20)))
print(df.columns) # RangeIndex(start=0, stop=20, step=1)
res = df.drop(np.r_[3, 9:15], 1)
print(res.columns)
# Int64Index([0, 1, 2, 4, 5, 6, 7, 8, 15, 16, 17, 18, 19], dtype='int64')
这篇关于删除 pandas 中的单个列和范围列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文