Pandas DataFrame.ix[] Function
Minahil Noor
Jan 30, 2023
Pandas
Pandas DataFrame
-
Syntax of
pandas.DataFrame.ix[]
: -
Example Codes:
DataFrame.ix[]
Method to Slice Row Index -
Example Codes:
DataFrame.ix[]
Method to Slice Column Index -
Example Codes:
DataFrame.ix[]
Method to Slice Column Label
![Pandas DataFrame.ix[] Function](/img/Python-Pandas/feature-image---Pandas-DataFrame-DataFrame.ix-Function.webp)
Warning
DataFrame.ix
is deprecated from Pandas version 0.20.0. You can use the more strict indexing method like loc
and iloc
.Python Pandas DataFrame.ix[]
function slices rows or columns depending upon the value of the parameters.
Syntax of pandas.DataFrame.ix[]
:
DataFrame.ix[index = None,
label = None]
Parameters
index |
An integer or list of integers for slicing row index. |
label |
A string, integer, list of string, or integer for slicing column labels. |
Return
It returns the modified DataFrame.
Example Codes: DataFrame.ix[]
Method to Slice Row Index
import pandas as pd
dataframe=pd.DataFrame({'Attendance': {0: 60, 1: 100, 2: 80,3: 78,4: 95},
'Name': {0: 'Olivia', 1: 'John', 2: 'Laura',3: 'Ben',4: 'Kevin'},
'Obtained Marks': {0: 90, 1: 75, 2: 82, 3: 64, 4: 45}})
print("The Original Data frame is: \n")
print(dataframe)
dataframe1 = dataframe.ix[:2, ]
print("The Modified Data frame is: \n")
print(dataframe1)
Output:
The Original Data frame is:
Attendance Name Obtained Marks
0 60 Olivia 90
1 100 John 75
2 80 Laura 82
3 78 Ben 64
4 95 Kevin 45
The Modified Data frame is:
Attendance Name Obtained Marks
0 60 Olivia 90
1 100 John 75
2 80 Laura 82
It has sliced row indexes 3
and 4
.
Example Codes: DataFrame.ix[]
Method to Slice Column Index
To slice the column of DataFrame
in Pandas, we will call the ix[]
function for the column label using the index.
import pandas as pd
dataframe=pd.DataFrame({'Attendance': {0: 60, 1: 100, 2: 80,3: 78,4: 95},
'Name': {0: 'Olivia', 1: 'John', 2: 'Laura',3: 'Ben',4: 'Kevin'},
'Obtained Marks': {0: 90, 1: 75, 2: 82, 3: 64, 4: 45}})
print("The Original Data frame is: \n")
print(dataframe)
dataframe1 = dataframe.ix[ : , :1]
print("The Modified Data frame is: \n")
print(dataframe1)
Output:
The Original Data frame is:
Attendance Name Obtained Marks
0 60 Olivia 90
1 100 John 75
2 80 Laura 82
3 78 Ben 64
4 95 Kevin 45
The Modified Data frame is:
Attendance
0 60
1 100
2 80
3 78
4 95
Now it has returned the first column of the DataFrame only.
Example Codes: DataFrame.ix[]
Method to Slice Column Label
We can also pass the column label as a parameter to keep that column and slice other columns.
import pandas as pd
dataframe=pd.DataFrame({'Attendance': {0: 60, 1: 100, 2: 80,3: 78,4: 95},
'Name': {0: 'Olivia', 1: 'John', 2: 'Laura',3: 'Ben',4: 'Kevin'},
'Obtained Marks': {0: 90, 1: 75, 2: 82, 3: 64, 4: 45}})
print("The Original Data frame is: \n")
print(dataframe)
dataframe1 = dataframe.ix[ : ,"Name"]
print("The Modified Data frame is: \n")
print(dataframe1)
Output:
The Original Data frame is:
Attendance Name Obtained Marks
0 60 Olivia 90
1 100 John 75
2 80 Laura 82
3 78 Ben 64
4 95 Kevin 45
The Modified Data frame is:
0 Olivia
1 John
2 Laura
3 Ben
4 Kevin
Name: Name, dtype: object
The function has sliced the other columns while keeping the Name
column. But you should notice that the function has kept Name
column values and sliced its label.
Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe