Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 提取两个值之间的列_Python_Pandas_Dataframe_Multiple Columns - Fatal编程技术网

Python 提取两个值之间的列

Python 提取两个值之间的列,python,pandas,dataframe,multiple-columns,Python,Pandas,Dataframe,Multiple Columns,我有如下数据。前两列是连续年份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 A

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

df.index.get\u loc('2005')您可以使用
np.r\ucode>

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: []


您可以使用
np.r\ucode>

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: []

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

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

df = pd.DataFrame(columns=list('ab') +
                [*map(str,pd.date_range('2000','2021',freq='y').year)])
用于获取感兴趣值的切片位置:

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的索引:

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组合在一起:

res = df.iloc[:,np.r_[A,B]]
res.columns
Index(['2005', '2006', '2007', '2008', '2009', '2010', '2015', '2016', '2017',
       '2018', '2019', '2020'],
      dtype='object')
我认为@anky使用的是正确的方法,而且非常灵活,这个答案只是一个替代方法,使用熊猫内置索引方法:

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

df = pd.DataFrame(columns=list('ab') +
                [*map(str,pd.date_range('2000','2021',freq='y').year)])
用于获取感兴趣值的切片位置:

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的索引:

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组合在一起:

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

将变量
col\u range
定义为
col\u range=list(range(2005、2011))
。现在使用-
df.loc[:,col_range]
,它将为您提供一个只包含2005…2010列的数据帧,将变量
col_range
定义为
col_range=list(range(2005,2011))
。现在使用-
df.loc[:,col_range]
,它将为您提供一个只包含2005…2010列的数据帧。此解决方案非常优雅。此解决方案非常优雅