The difference between cars.iloc [[3, 0]], cars.iloc [[3], [0]] and cars.iloc [3, 0]

I learn pandas and work on cars (csv file). I executed the following commands:

1) cars.iloc [[3, 0]]

Out[2]: 
cars_per_cap        country drives_right

18 India Invalid US 809 US True

2) cars.iloc [[3], [0]]

Out[7]: 
cars_per_cap

IN 18

3) cars.iloc [3, 0]

Out[9]: 18

I got confused in the first and third command, I checked the type of all, and the first 2 - DataFrame, and the third - not. However, why do I get different results for the 1st and 3rd? Any help would be appreciated.

+4
source share
1 answer
df = pd.DataFrame({'A':list('abcdef'),
                   'B':[4,5,4,5,5,4],
                   'C':[7,8,9,4,2,3],
                   'D':[1,3,5,7,1,0],
                   'E':[5,3,6,9,2,4],
                   'F':list('aaabbb')}, index=range(10, 16))

print (df)
    A  B  C  D  E  F
10  a  4  7  1  5  a
11  b  5  8  3  3  a
12  c  4  9  5  6  a
13  d  5  4  7  9  b
14  e  5  2  1  2  b
15  f  4  3  0  4  b

Select 3. and 0. rows, all columns:

print (df.iloc[[3,0]])
#same as 
#print (df.iloc[[3,0], :])
    A  B  C  D  E  F
13  d  5  4  7  9  b
10  a  4  7  1  5  a

Select columns 3. and 0. , all rows:

print (df.iloc[:, [3,0]])

    D  A
10  1  a
11  3  b
12  5  c
13  7  d
14  1  e
15  0  f

3. 0. -

print (df.iloc[[3],[0]])
    A
13  d

, , :

print (df.iloc[3,0])
d

:

3. 0. , 0. column - second [] DataFrame:

print (df.iloc[[3, 0],[0]])
    A
13  d
10  a

... , Series:

print (df.iloc[[3, 0], 0])
13    d
10    a
Name: A, dtype: object

:

print (df.iloc[[0], [3, 0]])
    D  A
10  1  a

print (df.iloc[0, [3, 0]])
D    1
A    a
Name: 10, dtype: object

seelct :

print (df.iloc[[3,0], [3,0]])
    D  A
13  7  d
10  1  a
+7

Source: https://habr.com/ru/post/1695095/


All Articles