JIYIK CN >

Current Location:Home > Learning > PROGRAM > Python >

Pandas DataFrame.ix[] Function

Author:JIYIK Last Updated:2025/05/01 Views:

Python Pandas DataFrame.ix[] function slices rows or columns based on the value of the argument.


pandas.DataFrame.ix[]grammar

DataFrame.ix[index = None,
             label = None]

parameter

index Integer or list of integers used to slice row indices.
label String, integer, list of strings, or integer, used for slice column labels.

return

It returns the modified DataFrame.


Sample code: DataFrame.ix[]How to split 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 ​​the row indices 3and 4.


Example code: DataFrame.ix[]Method for slicing column index

In Pandas, to slice columns of a DataFrame, we will ix[]call the slice function on the column labels using indexing.

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 only the first column of the DataFrame is returned.


Example code: DataFrame.ix[]How to split column labels

We can also pass the column label as an argument to keep that column and slice the 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 preserves Namethe column while slicing the other columns. But you should notice that the function preserves Namethe values ​​of the column and slices its labels.

Previous:Pandas DataFrame.describe() Function

Next: None

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Pandas DataFrame.describe() Function

Publish Date:2025/05/01 Views:120 Category:Python

Python Pandas DataFrame.describe() function returns the statistics of a DataFrame. pandas.DataFrame.describe() grammar DataFrame . describe( percentiles = None , include = None , exclude = None , datetime_is_numeric = False ) parameter perc

Pandas DataFrame.astype() Function

Publish Date:2025/05/01 Views:160 Category:Python

Python Pandas DataFrame.astype() function changes the data type of an object to the specified data type. pandas.DataFrame.astype() grammar DataFrame . astype(dtype, copy = True , errors = "raise" ) parameter dtype The data type we want to a

Pandas DataFrame.to_dict() Function

Publish Date:2025/05/01 Views:188 Category:Python

Python Pandas DataFrame.to_dict() function converts the given DataFrame to a dictionary. pandas.DataFrame.to_dict() Syntax DataFrame . to_dict(orient = 'dict' , into = class ' dict ' ) parameter orient This parameter determines the type of

Pandas DataFrame.reset_index() Function

Publish Date:2025/05/01 Views:140 Category:Python

Python Pandas DataFrame.reset_index() function resets the index of the given DataFrame. It replaces the old index with the default index. If the given DataFrame has a MultiIndex, then this method removes all the levels. pandas.DataFrame.rep

Pandas DataFrame.resample() Function

Publish Date:2025/05/01 Views:78 Category:Python

Python Pandas DataFrame.resample() function resamples time series data. pandas.DataFrame.resample() Syntax DataFrame . resample( rule, axis = 0 , closed = None , label = None , convention = "start" , kind = None , loffset = None , base = No

Pandas DataFrame.insert() Function

Publish Date:2025/05/01 Views:116 Category:Python

Python Pandas DataFrame.insert() function inserts a column at the specified position into the DataFrame. pandas.DataFrame.insert() Syntax DataFrame . insert(loc, column, value, allow_duplicates = False ) parameter loc It is an integer param

Pandas DataFrame.idxmax() Function

Publish Date:2025/05/01 Views:79 Category:Python

Python Pandas DataFrame.idxmax() function returns the index of the maximum value in a row or column. pandas.DataFrame.idxmax() Syntax DataFrame . idxmax(axis = 0 , skipna = True ) parameter axis It is a parameter of integer or string type.

Pandas DataFrame sort_index() Function

Publish Date:2025/05/01 Views:183 Category:Python

This tutorial explains how to use pandas.DataFrame.sort_index() the sort method to sort a Pandas DataFrame based on its index. We will use the DataFrame shown in the above example to explain how to sort a Pandas DataFrame based on the index

Pandas cut function

Publish Date:2025/05/01 Views:165 Category:Python

pandas.cut() The function can distribute the given data into a range, which can also be called bins . We will use the following DataFrame in this article. import pandas as pd df = pd . DataFrame( { "Name" : [ "Anish" , "Birat" , "Chirag" ,

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial