slicing with iloc and negative integer in Pandas

Viewed 2567
1 Answers

It means, get all columns except the last column:

df = pd.DataFrame(np.random.randint(0,100,(5,5)), index=[*'abcde'], columns=[*'ABCDE'])

df.iloc[:,:-1]

Output:

    A   B   C   D
a  79  23   9  89
b  67  60  32  82
c  66  18  41  67
d  90  51  63  29
e  34  65  82  82

This statement gets all rows and slices the columns to filter out the last. And, there is no error by your second statement it is good statement.

df.iloc[:, 0]

Output:

a    79
b    67
c    66
d    90
e    34
Name: A, dtype: int3

Get all rows of the first column (position 0).

Related