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

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically