2022年2月7日 星期一

[python, iloc, select] How to get the latest and the rest of column serials



ref

  1. The slice notation

a[start:stop]  # items start through stop-1

a[start:]         # items start through the rest of the array

a[:stop]         # items from the beginning through stop-1

a[:]                 # a copy of the whole array


Reference

https://stackoverflow.com/questions/509211/understanding-slice-notation


mydict = [{'a': 1, 'b': 2, 'c': 3, 'd': 4},

           {'a': 100, 'b': 200, 'c': 300, 'd': 400}

           {'a': 1000, 'b': 2000, 'c': 3000, 'd': 4000 }]


import pandas as pd

data = pd.DataFrame(mydict)


>>> data

      a     b     c     d

0     1     2     3     4

 100   200   300   400

1000  2000  3000  4000



# Get the latest column serials

# Row selection: : <-- a copy whole row

# Col selection: -1 <-- choose the latest column

y = data.iloc[:, -1]  

>>> y

0       4

1     400

2    4000



# Get the rest of the column serials

# Row selection: : <-- a copy whole row

# Col selection: :-1 <-- select whole column without the latest one

X = data.iloc[:,:-1]


>>> X

      a     b     c

0     1     2     3

 100   200   300

1000  2000  3000