Reputation: 3296
I want the columns of a dataframe between two indices, endDate
and totalCurrentAssets
but excluding them. So I tried with .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
Upvotes: 3
Views: 39
Reputation: 30971
Start from retrieving integer indices (numbers) of the columns in question:
n1 = df.columns.get_loc('endDate')
n2 = df.columns.get_loc('totalCurrentAssets')
Then use iloc with these indices, respectively corrected:
df.iloc[:, n1 + 1 : n2]
This time column with integer number n2 will not be retrieved.
Upvotes: 2
Reputation: 88236
One way could be to chain loc
with an iloc
to take a further slice:
df.loc[:,'endDate':'totalCurrentAssets'].iloc[:,1:-1]
Or you could also use Index.get_loc
:
cols = df.columns
df.iloc[:, cols.get_loc('endDate')+1 : cols.get_loc('totalCurrentAssets')]
Upvotes: 3