Its output
is as follows −
B D
1 0.890791 0.631615
3 -1.284314 -0.026251
5 -0.512888 -0.518930
A B C D
1 -0.685354 0.890791 -0.813012 0.631615
2 -0.783192 -0.531378 0.025070 0.230806
B C
0 0.256239 -1.270702
1 0.890791 -0.813012
2 -0.531378 0.025070
3 -1.284314 0.826977
4 -0.460729 1.423332
5 -0.512888 0.581409
6 -1.204853 0.098060
7 -0.947857 0.641358
.ix():
Besides pure label based and integer based, Pandas provides a hybrid method for selections and subsetting the object using the .ix() operator.
Example 1:
import
pandas as
pd
import
numpy as
np
df =
pd.
DataFrame
(
np.
random.
randn(
8
,
4
),
columns =
[
'A'
,
'B'
,
'C'
,
'D'
])
# Integer slicing
print
df.
ix[:
4
]
Its output
is as follows −
A B C D
0 0.699435 0.256239 -1.270702 -0.645195
1 -0.685354 0.890791 -0.813012 0.631615
2 -0.783192 -0.531378 0.025070 0.230806
3 0.539042 -1.284314 0.826977 -0.026251
Example 2:
import
pandas as
pd
import
numpy as
np
df =
pd.
DataFrame
(
np.
random.
randn(
8
,
4
),
columns =
[
'A'
,
'B'
,
'C'
,
'D'
])
# Index slicing
print
df.
ix[:,
'A'
]
Its output
is as follows −
0 0.699435
1 -0.685354
2 -0.783192
3 0.539042
4 -1.044209
5 -1.415411
6 1.062095
7 0.994204
Name: A, dtype: float64
Use of Notations:
Getting values from the Pandas object with Multi-axes indexing uses the following notation −
Object
|
Indexers
|
Return Type
|
Series
|
s.loc[indexer]
|
Scalar value
|
DataFrame
|
df.loc[row_index,col_index]
|
Series object
|
Panel
|
p.loc[item_index,major_index, minor_index]
|
p.loc[item_index,major_index, minor_index]
|
Note − .iloc() & .ix()
applies the same indexing options and Return value.
Let us now see how each operation can be performed on the DataFrame object. We will use the basic indexing operator '[ ]' −
Example 1:
import
pandas as
pd
import
numpy as
np
df =
pd.
DataFrame
(
np.
random.
randn(
8
,
4
),
columns =
[
'A'
,
'B'
,
'C'
,
'D'
])
print
df[
'A'
]
Its output
is as follows −
0 -0.478893
1 0.391931
2 0.336825
3 -1.055102
4 -0.165218
5 -0.328641
6 0.567721
7 -0.759399
Name: A, dtype: float64
Note
− We can pass a list of values to [ ] to select those columns.
Example 2:
import
pandas as
pd
import
numpy as
np
df =
pd.
DataFrame
(
np.
random.
randn(
8
,
4
),
columns =
[
'A'
,
'B'
,
'C'
,
'D'
])
print
df[[
'A'
,
'B'
]]
Its output
is as follows −
A B
0 -0.478893 -0.606311
1 0.391931 -0.949025
2 0.336825 0.093717
3 -1.055102 -0.012944
4 -0.165218 1.550310
5 -0.328641 -0.226363
6 0.567721 -0.312585
7 -0.759399 -0.372696
Example 3:
import
pandas as
pd
import
numpy as
np
df =
pd.
DataFrame
(
np.
random.
randn(
8
,
4
),
columns =
[
'A'
,
'B'
,
'C'
,
'D'
])
print
df[
2
:
2
]
Its output
is as follows −
Columns: [A, B, C, D]
Index: []
Attribute Access:
Columns can be selected using the attribute operator '.'.
Example:
import
pandas as
pd
import
numpy as
np
df =
pd.
DataFrame
(
np.
random.
randn(
8
,
4
),
columns =
[
'A'
,
'B'
,
'C'
,
'D'
])
print
df.
A
Its output
is as follows −
0 -0.478893
1 0.391931
2 0.336825
3 -1.055102
4 -0.165218
5 -0.328641
6 0.567721
7 -0.759399
Name: A, dtype: float64