我有如下数据。前两列是连续年份df[3:60]列的字符串和列名。如何提取2005年:2010年和2015年之间的所有列
Country Indicator 1960 1961 1962 1963.....
Aruba US$ 15678 156789 156790 156791
Afgha US$ 68239 78239 88239 98239
Angola US$ 45678 55678 65678 75678
Albania US$ 89345 99345 109345 119345
Andorra US$ 62790 72790 82790 92790
Arab US$ 12987 22987 32987 42987
UAE US$ 6047 16047 26047 36047
我试着提取列的索引
df.index.get_loc('2005') <- 45
df.index.get_loc('2010') <- 50
df.index.get_loc('2015') <- 55
df.iloc[:, [45:50,55:]]
上面的代码显示了一个错误。如何使用索引范围提取多个列
您可以使用
np.r_
:例如:
我认为@anky使用np.r_是正确的方法,而且非常灵活,这个答案只是一个替代方法,使用pandas内置索引方法:
注意:我正在使用@anky的示例数据帧:
使用slice_indexer获取感兴趣值的切片位置:
添加A和B的iloc索引:
还要注意@anky的方法会更有效,因为iloc只被调用一次。同样,这只是一个可用方法的游戏
当然,您可以将A和B的np.r_与slices组合起来:
相关问题 更多 >
编程相关推荐