Python 如何从其他两列之间的列中获取数据帧的行?
我想要两个索引之间的数据帧列,Python 如何从其他两列之间的列中获取数据帧的行?,python,python-3.x,pandas,dataframe,Python,Python 3.x,Pandas,Dataframe,我想要两个索引之间的数据帧列,endDate和totalCurrentAssets,但不包括它们。所以我尝试了.iloc: >>>ticker.balance_sheet().loc[:, 'endDate':'totalCurrentAssets'] endDate cash shortTermInvestments netReceivables inventory otherCurrentAssets totalCurrentA
endDate
和totalCurrentAssets
,但不包括它们。所以我尝试了.iloc
:
>>>ticker.balance_sheet().loc[:, 'endDate':'totalCurrentAssets']
endDate cash shortTermInvestments netReceivables inventory otherCurrentAssets totalCurrentAssets
symbol row
MSFT 0 2019-06-30 11356000000 122476000000 29524000000 2063000000 10133000000 175552000000
1 2018-06-30 11946000000 121718000000 26481000000 2662000000 6855000000 169662000000
2 2017-06-30 7663000000 125238000000 22431000000 2181000000 5183000000 162696000000
3 2016-06-30 6510000000 106531000000 18277000000 2251000000 6091000000 139660000000
cols = df.columns
df.iloc[:, cols.get_loc('endDate')+1 : cols.get_loc('totalCurrentAssets')]
一种方法是使用
iloc
链接loc
,以获取进一步的切片:
df.loc[:,'endDate':'totalCurrentAssets'].iloc[:,1:-1]
或者您也可以使用索引。获取位置:
>>>ticker.balance_sheet().loc[:, 'endDate':'totalCurrentAssets']
endDate cash shortTermInvestments netReceivables inventory otherCurrentAssets totalCurrentAssets
symbol row
MSFT 0 2019-06-30 11356000000 122476000000 29524000000 2063000000 10133000000 175552000000
1 2018-06-30 11946000000 121718000000 26481000000 2662000000 6855000000 169662000000
2 2017-06-30 7663000000 125238000000 22431000000 2181000000 5183000000 162696000000
3 2016-06-30 6510000000 106531000000 18277000000 2251000000 6091000000 139660000000
cols = df.columns
df.iloc[:, cols.get_loc('endDate')+1 : cols.get_loc('totalCurrentAssets')]
从检索相关列的整数索引(编号)开始:
n1 = df.columns.get_loc('endDate')
n2 = df.columns.get_loc('totalCurrentAssets')
然后将iloc与这些指数一起使用,分别进行校正:
df.iloc[:, n1 + 1 : n2]
将不检索此整数为n2的时间列