
- Python Pandas - Home
- Python Pandas - Introduction
- Python Pandas - Environment Setup
- Python Pandas - Basics
- Python Pandas - Introduction to Data Structures
- Python Pandas - Index Objects
- Python Pandas - Panel
- Python Pandas - Basic Functionality
- Python Pandas - Indexing & Selecting Data
- Python Pandas - Series
- Python Pandas - Series
- Python Pandas - Slicing a Series Object
- Python Pandas - Attributes of a Series Object
- Python Pandas - Arithmetic Operations on Series Object
- Python Pandas - Converting Series to Other Objects
- Python Pandas - DataFrame
- Python Pandas - DataFrame
- Python Pandas - Accessing DataFrame
- Python Pandas - Slicing a DataFrame Object
- Python Pandas - Modifying DataFrame
- Python Pandas - Removing Rows from a DataFrame
- Python Pandas - Arithmetic Operations on DataFrame
- Python Pandas - IO Tools
- Python Pandas - IO Tools
- Python Pandas - Working with CSV Format
- Python Pandas - Reading & Writing JSON Files
- Python Pandas - Reading Data from an Excel File
- Python Pandas - Writing Data to Excel Files
- Python Pandas - Working with HTML Data
- Python Pandas - Clipboard
- Python Pandas - Working with HDF5 Format
- Python Pandas - Comparison with SQL
- Python Pandas - Data Handling
- Python Pandas - Sorting
- Python Pandas - Reindexing
- Python Pandas - Iteration
- Python Pandas - Concatenation
- Python Pandas - Statistical Functions
- Python Pandas - Descriptive Statistics
- Python Pandas - Working with Text Data
- Python Pandas - Function Application
- Python Pandas - Options & Customization
- Python Pandas - Window Functions
- Python Pandas - Aggregations
- Python Pandas - Merging/Joining
- Python Pandas - MultiIndex
- Python Pandas - Basics of MultiIndex
- Python Pandas - Indexing with MultiIndex
- Python Pandas - Advanced Reindexing with MultiIndex
- Python Pandas - Renaming MultiIndex Labels
- Python Pandas - Sorting a MultiIndex
- Python Pandas - Binary Operations
- Python Pandas - Binary Comparison Operations
- Python Pandas - Boolean Indexing
- Python Pandas - Boolean Masking
- Python Pandas - Data Reshaping & Pivoting
- Python Pandas - Pivoting
- Python Pandas - Stacking & Unstacking
- Python Pandas - Melting
- Python Pandas - Computing Dummy Variables
- Python Pandas - Categorical Data
- Python Pandas - Categorical Data
- Python Pandas - Ordering & Sorting Categorical Data
- Python Pandas - Comparing Categorical Data
- Python Pandas - Handling Missing Data
- Python Pandas - Missing Data
- Python Pandas - Filling Missing Data
- Python Pandas - Interpolation of Missing Values
- Python Pandas - Dropping Missing Data
- Python Pandas - Calculations with Missing Data
- Python Pandas - Handling Duplicates
- Python Pandas - Duplicated Data
- Python Pandas - Counting & Retrieving Unique Elements
- Python Pandas - Duplicated Labels
- Python Pandas - Grouping & Aggregation
- Python Pandas - GroupBy
- Python Pandas - Time-series Data
- Python Pandas - Date Functionality
- Python Pandas - Timedelta
- Python Pandas - Sparse Data Structures
- Python Pandas - Sparse Data
- Python Pandas - Visualization
- Python Pandas - Visualization
- Python Pandas - Additional Concepts
- Python Pandas - Caveats & Gotchas
Python Pandas - Accessing DataFrame
Pandas DataFrame is a two-dimensional labeled data structure with rows and columns labels, it is looks and works similar to a table in a database or a spreadsheet. To work with the DataFrame labels, pandas provides simple tools to access and modify the rows and columns using index the index and columns attributes of a DataFrame.
In this tutorial, we will learn about how to access and modify rows and columns in a Pandas DataFrame using the index and columns attributes of the DataFrame.
Accessing the DataFrame Rows Labels
The index attribute in Pandas is used to access row labels in a DataFrame. It returns an index object containing the series of labels corresponding to the data represented in the each row of the DataFrame. These labels can be integers, strings, or other hashable types.
Example
The following example access the DataFrame row labels using the pd.index attribute.
import pandas as pd # Create a DataFrame df = pd.DataFrame({ 'Name': ['Steve', 'Lia', 'Vin', 'Katie'], 'Age': [32, 28, 45, 38], 'Gender': ['Male', 'Female', 'Male', 'Female'], 'Rating': [3.45, 4.6, 3.9, 2.78]}, index=['r1', 'r2', 'r3', 'r4']) # Access the rows of the DataFrame result = df.index print('Output Accessed Row Labels:', result)
Following is the output of the above code −
Output Accessed Row Labels: Index(['r1', 'r2', 'r3', 'r4'], dtype='object')
Modifying DataFrame Row Labels
With the index attribute you can also modify the row labels of a DataFrame.
Example
Here is an example that demonstrates accessing and modifying the row labels of the Pandas DataFrame using the index attribute.
import pandas as pd # Create a DataFrame df = pd.DataFrame({ 'Name': ['Steve', 'Lia', 'Vin', 'Katie'], 'Age': [32, 28, 45, 38], 'Gender': ['Male', 'Female', 'Male', 'Female'], 'Rating': [3.45, 4.6, 3.9, 2.78]}, index=['r1', 'r2', 'r3', 'r4']) # Display the Input DataFrame print('Input DataFrame:\n', df) # Modify the Row labels of the DataFrame df.index = [100, 200, 300, 400] print('Output Modified DataFrame with the updated index labels:\n', df)
On executing the above code you will get the following output −
Input DataFrame:
Name | Age | Gender | Rating | |
---|---|---|---|---|
r1 | Steve | 32 | Male | 3.45 |
r2 | Lia | 28 | Female | 4.60 |
r3 | Vin | 45 | Male | 3.90 |
r4 | Katie | 38 | Female | 2.78 |
Name | Age | Gender | Rating | |
---|---|---|---|---|
100 | Steve | 32 | Male | 3.45 |
200 | Lia | 28 | Female | 4.60 |
300 | Vin | 45 | Male | 3.90 |
400 | Katie | 38 | Female | 2.78 |
Accessing The DataFrame Columns Labels
The Pandas pd.columns attribute is used to access the labels of the columns in the DataFrame. You can access and modify these column labels similarly to how we work with row labels.
Example
The following example demonstrates how to access the DataFrame column labels using the pd.columns attribute.
import pandas as pd # Create a DataFrame df = pd.DataFrame({ 'Name': ['Steve', 'Lia', 'Vin', 'Katie'], 'Age': [32, 28, 45, 38], 'Gender': ['Male', 'Female', 'Male', 'Female'], 'Rating': [3.45, 4.6, 3.9, 2.78]}, index=['r1', 'r2', 'r3', 'r4']) # Access the column labels of the DataFrame result = df.columns print('Output Accessed column Labels:', result)
Following is the output of the above code −
Output Accessed column Labels: Index(['Name', 'Age', 'Gender', 'Rating'], dtype='object')
Modifying the DataFrame Column Labels
Column labels can be modified using the columns attribute.
Example
This example demonstrates how to access and modify the DataFrame column labels using the pd.columns attribute.
import pandas as pd # Create a DataFrame df = pd.DataFrame({ 'Name': ['Steve', 'Lia', 'Vin', 'Katie'], 'Age': [32, 28, 45, 38], 'Gender': ['Male', 'Female', 'Male', 'Female'], 'Rating': [3.45, 4.6, 3.9, 2.78]}, index=['r1', 'r2', 'r3', 'r4']) # Display the Input DataFrame print('Input DataFrame:\n', df) # Modify the Column labels of the DataFrame df.columns = ['Col1', 'Col2', 'Col3', 'Col4'] print('Output Modified DataFrame with the updated Column Labels\n:', df)
Following is the output of the above code −
Input DataFrame:
Name | Age | Gender | Rating | |
---|---|---|---|---|
r1 | Steve | 32 | Male | 3.45 |
r2 | Lia | 28 | Female | 4.60 |
r3 | Vin | 45 | Male | 3.90 |
r4 | Katie | 38 | Female | 2.78 |
Col1 | Col2 | Col3 | Col4 | |
---|---|---|---|---|
r1 | Steve | 32 | Male | 3.45 |
r2 | Lia | 28 | Female | 4.60 |
r3 | Vin | 45 | Male | 3.90 |
r4 | Katie | 38 | Female | 2.78 |