通过列号 pandas 选择不相邻的列 [英] Selecting non-adjacent columns by column number pandas
本文介绍了通过列号 pandas 选择不相邻的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我还没有在任何地方找到答案.我正在尝试选择列号2
和86:100
.显然,我宁愿不按标签选择它们.
I have yet to find an answer for this anywhere. I am attempting to select columns number 2
and 86:100
. Obviously, I would rather not select them by label.
直觉上我尝试:df_new = df.iloc[:,[2,86:100]]
无济于事.
Intuitively I have tried: df_new = df.iloc[:,[2,86:100]]
to no avail.
选择这些列的最有效方法是什么?
What is the most efficient way of selecting these columns?
推荐答案
You can use np.r_
to combine slices:
df = pd.DataFrame(np.random.random((3, 10)))
res = df.iloc[:, np.r_[2, 5:10]]
print(res)
2 5 6 7 8 9
0 0.489923 0.406723 0.085721 0.235617 0.724768 0.398237
1 0.697457 0.565602 0.177975 0.215762 0.377650 0.658344
2 0.116625 0.770128 0.930788 0.367666 0.044933 0.486751
这篇关于通过列号 pandas 选择不相邻的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文