Pandas DataFrame.ix[] Function

Pandas DataFrame.ix[] Function

Minahil Noor Mar-30, 2021 Jan-09, 2021 Pandas Pandas DataFrame
  1. Syntax of pandas.DataFrame.ix[]:
  2. Example Codes: DataFrame.ix[] Method to Slice Row Index
  3. Example Codes: DataFrame.ix[] Method to Slice Column Index
  4. Example Codes: DataFrame.ix[] Method to Slice Column Label
<div class="panel panel-primary panel-warning">
<div class="panel-heading">Warning</div>
<div class="panel-body">

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.

Related Article - Pandas DataFrame

  • Pandas concat Function
  • Pandas cut Function
  • Pandas DataFrame sort_index() Function
  • Pandas DataFrame.idxmax() Function
  • Pandas DataFrame.insert() Function
  • Pandas DataFrame.resample() Function