在Pandas iloc中同时传递列索引和范围 [英] Pass column index and range simultaneously in Pandas iloc
本文介绍了在Pandas iloc中同时传递列索引和范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个包含100列的数据框。我只想为所有行选择0,1和51-100列。到目前为止,我正在尝试类似的操作:
Suppose I have a dataframe with 100 columns. I want to select only 0,1 and 51-100 columns for all rows. I am trying something like this as of now:
df.iloc[:,[0,1,range(51,101)]]
但这会引发错误:
用序列设置数组元素
如果我仅传递范围而不传递列索引,则可以正常工作。
Though it is working if I am passing only range and not column indices.
我也尝试过:
df.iloc[:,[0,1,51:102)]]
它显示的语法无效。
有人可以在这里指出错误。还请提出实现功能的替代方法。
Can somebody please point out mistake here. Also please suggest alternative way to achieve functionality.
推荐答案
我认为您需要 numpy.r _
用于并发索引:
I think you need numpy.r_
for concanecate indices:
df.iloc[:,np.r_[0,1,51:102]]
示例:
df = pd.DataFrame([[0] * 10], columns=list('abcdefghij'))
print (df)
a b c d e f g h i j
0 0 0 0 0 0 0 0 0 0 0
print (df.iloc[:,np.r_[0,1,5:8]])
a b f g h
0 0 0 0 0 0
这篇关于在Pandas iloc中同时传递列索引和范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文