提取两个值之间的列

2024-10-04 11:29:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我有如下数据。前两列是连续年份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:]]

上面的代码显示了一个错误。如何使用索引范围提取多个列


Tags: 数据字符串dfgetindexcountrylocindicator
2条回答

您可以使用np.r_

a = df.columns.get_loc('2005')
b = df.columns.get_loc('2010')
c = df.columns.get_loc('2015')

df.iloc[:,np.r_[a-1:b,c-1:len(df.columns)]]

例如:

df = pd.DataFrame(columns=list('ab') +
                [*map(str,pd.date_range('2000','2021',freq='y').year)])
print(df)

Empty DataFrame
Columns: [a, b, 2000, 2001, 2002, 2003, 2004, 2005,
         2006, 2007, 2008, 2009, 2010, 2011, 2012, 
         2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020]
Index: []

print(df.iloc[:,np.r_[a-1:b,c-1:len(df.columns)]])

Empty DataFrame
Columns: [2005, 2006, 2007, 2008, 2009, 2010, 2015, 2016, 2017, 2018, 2019, 2020]
Index: []

我认为@anky使用np.r_是正确的方法,而且非常灵活,这个答案只是一个替代方法,使用pandas内置索引方法:

注意:我正在使用@anky的示例数据帧:

df = pd.DataFrame(columns=list('ab') +
                [*map(str,pd.date_range('2000','2021',freq='y').year)])

使用slice_indexer获取感兴趣值的切片位置:

A = df.columns.slice_indexer('2005','2010')
A
slice(7, 13, None)
#if one entry is included, it includes the location of the last index
B = df.columns.slice_indexer('2015')
B
slice(17, 23, None)

添加A和B的iloc索引:

res = df.iloc[:,A] + df.iloc[:,B]
res
Index(['2005', '2006', '2007', '2008', '2009', '2010', '2015', '2016', '2017',
       '2018', '2019', '2020'],
      dtype='object')

还要注意@anky的方法会更有效,因为iloc只被调用一次。同样,这只是一个可用方法的游戏

当然,您可以将A和B的np.r_slices组合起来:

res = df.iloc[:,np.r_[A,B]]
res.columns
Index(['2005', '2006', '2007', '2008', '2009', '2010', '2015', '2016', '2017',
       '2018', '2019', '2020'],
      dtype='object')

相关问题 更多 >