Change language

Python | Pandas DataFrame.ix []

| |

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:

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method