简体   繁体   中英

How to get the rows of a dataframe from columns between two other columns?

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

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')]

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM