【发布时间】:2022-01-17 22:55:08
【问题描述】:
df = pd.DataFrame({'name':['jack', 'james', 'joe'], 'age':[30, 40, 50], 'height': [6.0, 5.9, 5.5], 'is_married':[1,0,1], 'is_parent':[0,1,1]})
我只想选择列索引:0、2、3、4(但不是 1)。以下工作正常:
df_cut = df.iloc[:,[0,2,3,4]]
但不是传递每个特定的列索引,而是寻找一种可以传递的方法。我试过了:
df_cut = df.iloc[:,[0,[2:4]]
但它不起作用。最好的解决方案是什么?
【问题讨论】:
标签: python pandas data-analysis