Python | Pandas DataFrame.ix []

File handling | NumPy | Python Methods and Functions

Pandas DataFrame.ix [] — it is a label-based slicing technique and DataFrame.ix [] . In addition to pure label and integer based data, Pandas provides a hybrid method for selecting and subsetting an object using the ix [] operator.  ix [] is the most general indexer and will support any input in iloc [] .

Syntax: DataFrame.ix []

Parameters:
Index Position : Index position of rows in integer or list of integer.
Index label: String or list of string of index label of rows

Returns: Data frame or Series depending on parameters

Code # 1:

# pandas package import

import pandas as geek

 
# create a data frame from a CSV file

data = geek.read_csv ( " https://media.python.engineering/wp-content/uploads/nba.csv "

 
# Integer slicing

print ( "Slicing only rows (till index 4):" )

x1 = data.ix [: 4 ,]

print (x1, " " )

 

print ( "Slicing rows and columns (rows = 4, col 1-4, excluding 4): " )

x2 = data.ix [: 4 , 1 : 4 ]

print (x2)

Output:

Code no. 2:

# pandas package import

import pandas as geek

 
# create data frame from CSV file

data = geek.read_csv ( "nba.csv"

 
# Index slicing by column Height

print ( "After index slicing:" )

x1 = data.ix [ 10 : 20 , `Height` ]

print (x1, " " )

 
# Column slicing index Salary

x2 = data.ix [ 10 : 20 , `Salary` ]

print (x2)

Output:

Code # 3:

# pandas and numpy imports

import pandas as pd

import numpy as np

 

df = pd.DataFrame (np.random.randn ( 10 , 4 ),

columns = [ `A` , `B` , ` C` , `D` ])

 

print ( "Original DataFrame:" , df)

 
# Integer slicing

print ( "Slicing only rows:" )

print ( " --------------------- ----- " )

x1 = df.ix [: 4 ,]

print (x1)

  

print ( "Slicing rows and columns: " )

print ( "----------------------------" )

x2 = df.ix [: 4 , 1 : 3 ]

print (x2)

Output:

Code # 4:

# pandas and numpy imports

import pandas as pd

import numpy as np

 

df = pd.DataFrame (np.random.randn ( 10 , 4 ),

columns = [ `A` , ` B` , `C` , ` D` ])

 

print ( "Original DataFrame:" , df)

 
# Integer slicing (prints all rows in a column & # 39; A & # 39;)

print ( " After index slicing (On `A`):" )

print ( "---------- ---------------- " )

x = df.ix [:, `A` ]

 

print (x)

Output:





Get Solution for free from DataCamp guru