熊猫选择单独的列

舞会2

给定以下数据框:

import pandas as pd
df = pd.DataFrame({'a':[1,2,3],'b':[4,5,6],'c':[1,2,3],'d':[4,5,6]})

df
   a  b  c  d
0  1  4  1  4
1  2  5  2  5
2  3  6  3  6

我只想按列索引位置选择列a,c和d。我没有运气就尝试了以下方法:

df[df.columns[0],df.columns[-2:]]

我可能可以通过常规Python找出答案,但想知道熊猫中是否有捷径。

BEN_YO

使用索引 append

df.loc[:,df.columns[[0]].append(df.columns[-2:])]
Out[1363]: 
   a  c  d
0  1  1  4
1  2  2  5
2  3  3  6

或者也许只是使用 .iloc

df.iloc[:,[0,2,3]] 

也许吧 np.r_

df.iloc[:,np.r_[0,-2:0]]
Out[1371]: 
   a  c  d
0  1  1  4
1  2  2  5
2  3  3  6

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章