2024-03-29 10:37:28 发布
网友
我想按df[:,(1,5:)]选择一个数据帧,即所有行和列编号1和列5到最后。在
df[:,(1,5:)]
我试过:
df.loc[1:3,('col_1','col_5':)]
有什么简单的方法可以用列号或列名(两者都有)?在
我认为您需要^{}作为concanate索引,而{}用于动态获取最后一列的位置:
df.iloc[1:3,np.r_[1, 5:len(df.columns)]]
样品:
如果需要从1到结尾的索引:
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到结尾):
np.r_
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
我们可以使用np.r_[]方法,该方法:
translates slice objects to concatenation along the first axis.
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
我认为您需要^{} 作为concanate索引,而{}用于动态获取最后一列的位置:
样品:
^{pr2}$如果需要从
1
到结尾的索引:也可以使用
np.r_
作为索引(选择第一个索引值,从3到结尾):我们可以使用np.r_[]方法,该方法:
相关问题 更多 >
编程相关推荐