2017-07-17 108 views
2

我想通过df[:,(1,5:)]选择一个数据帧,即所有行和列号为1,列5为结尾。熊猫数据帧按列索引

我曾尝试:

df.loc[1:3,('col_1','col_5':)] 

任何简单的方法通过列号或列名(两者)办呢?

回答

1

我认为你需要numpy.r_为concanecate指数和len(df.columns)为动态获取最后一栏的位置:

df.iloc[1:3,np.r_[1, 5:len(df.columns)]] 

样品:

np.random.seed(23) 
df = pd.DataFrame(np.random.randint(10, size=(5,11)), columns=list('ABCDEFGHIJK')) 
print (df) 
    A B C D E F G H I J K 
0 3 6 8 9 6 8 7 9 3 6 1 
1 2 5 5 0 5 0 9 9 3 1 7 
2 4 1 1 4 6 7 3 6 9 2 3 
3 0 8 6 6 0 5 6 0 2 0 3 
4 0 6 2 2 5 7 9 4 7 7 3 

df = df.iloc[1:3,np.r_[1, 5:len(df.columns)]] 
print (df) 
    B F G H I J K 
1 5 0 9 9 3 1 7 
2 1 7 3 6 9 2 3 

如果从1需求指数结束:

df = df.iloc[1:,np.r_[1, 5:len(df.columns)]] 
print (df) 
    B F G H I J K 
1 5 0 9 9 3 1 7 
2 1 7 3 6 9 2 3 
3 8 5 6 0 2 0 3 
4 6 7 9 4 7 7 3 

也可以使用np.r_为指数(选择第一索引值与从3至端):

df = df.iloc[np.r_[1, 3:len(df.index)],np.r_[1, 5:len(df.columns)]] 
print (df) 
    B F G H I J K 
1 5 0 9 9 3 1 7 
3 8 5 6 0 2 0 3 
4 6 7 9 4 7 7 3 
+0

谢谢,它适用于我iloc,但不适用于禄。 – moreo

+0

我也改变它;) – jezrael

2

我们可以使用np.r_[]方法,其中:

转换切片对象沿第一轴串联。

In [86]: df 
Out[86]: 
     col_0  col_1  col_2  col_3  col_4  col_5  col_6  col_7  col_8  col_9 
0 0.167483 0.104568 0.636430 0.706476 0.031586 0.936212 0.051971 0.541296 0.709061 0.870969 
1 0.714087 0.801728 0.339450 0.814825 0.080115 0.894817 0.547592 0.817298 0.452318 0.643578 
2 0.526403 0.731590 0.081630 0.060352 0.247103 0.159545 0.871784 0.219214 0.975865 0.336896 
3 0.182118 0.789699 0.658708 0.498196 0.555364 0.719202 0.228455 0.996334 0.974793 0.650326 
4 0.199542 0.680228 0.072198 0.030653 0.257683 0.462623 0.868273 0.727169 0.742707 0.425493 
5 0.345935 0.371039 0.987650 0.040109 0.867031 0.578675 0.438615 0.725258 0.486669 0.873423 
6 0.900702 0.421721 0.276828 0.592350 0.912363 0.210662 0.622967 0.631560 0.733113 0.131568 
7 0.715825 0.909033 0.179683 0.237543 0.971395 0.180977 0.854385 0.492278 0.247231 0.870750 
8 0.445305 0.514817 0.359233 0.592951 0.163524 0.391082 0.969412 0.258133 0.656737 0.325190 
9 0.773473 0.130874 0.969821 0.453790 0.236050 0.073497 0.169758 0.519774 0.337003 0.828883 

In [87]: df.iloc[:, np.r_[1, 5:df.shape[1]]] 
Out[87]: 
     col_1  col_5  col_6  col_7  col_8  col_9 
0 0.104568 0.936212 0.051971 0.541296 0.709061 0.870969 
1 0.801728 0.894817 0.547592 0.817298 0.452318 0.643578 
2 0.731590 0.159545 0.871784 0.219214 0.975865 0.336896 
3 0.789699 0.719202 0.228455 0.996334 0.974793 0.650326 
4 0.680228 0.462623 0.868273 0.727169 0.742707 0.425493 
5 0.371039 0.578675 0.438615 0.725258 0.486669 0.873423 
6 0.421721 0.210662 0.622967 0.631560 0.733113 0.131568 
7 0.909033 0.180977 0.854385 0.492278 0.247231 0.870750 
8 0.514817 0.391082 0.969412 0.258133 0.656737 0.325190 
9 0.130874 0.073497 0.169758 0.519774 0.337003 0.828883 
+1

很酷,与ILOC完美配合 – moreo