Renaming Pandas dataframe sections is a typical practice particularly when we are keen on offering a few bits of knowledge to others and groups. This implies that we might wish to make segment names more significant so it would be more straightforward for perusers to relate them to explicit settings.
Table of Contents
About Pandas DataFrame:
Pandas DataFrame is rectangular matrices that are utilized to store information. It is not difficult to picture and work with information when put away in dataFrame.
Consideration nerd! Reinforce your establishments with the Python Programming Foundation Course and become familiar with the fundamentals. In the first place, your meeting arrangements Enhance your Data Structures ideas with the Python DS Course. What’s more, in any case, your Machine Learning Journey, join the Machine Learning – Basic Level Course
It comprises lines and sections.
Each line is an estimation of some occurrence while the segment is a vector that contains information for some particular characteristic/variable.
Each data frame section has homogeneous information all through a particular segment yet data frame lines can contain homogeneous or heterogeneous information all through a particular column.
In contrast to two-dimensional clusters, pandas data frame tomahawks are marked.
Technique #1: Using rename() work.
One method of renaming the segments in a Pandas data frame is by utilizing the rename() work. This technique is very valuable when we really want to rename some chosen segments since we really want to indicate data just for the segments which are to be renamed.
Rename a solitary segment.
# Import pandas package
import
pandas as pd
# Define a dictionary containing ICC rankings
rankings
=
{
'test'
: [
'India'
,
'South Africa'
,
'England'
,
'New Zealand'
,
'Australia'
],
'odi'
: [
'England'
,
'India'
,
'New Zealand'
,
'South Africa'
,
'Pakistan'
],
't20'
: [
'Pakistan'
,
'India'
,
'Australia'
,
'England'
,
'New Zealand'
]}
# Convert the dictionary into DataFrame
rankings_pd
=
pd.DataFrame(rankings)
# Before renaming the columns
print
(rankings_pd)
rankings_pd.rename(columns
=
{
'test'
:
'TEST'
}, inplace
=
True
)
# After renaming the columns
print
(
"\nAfter modifying first column:\n"
, rankings_pd.columns)
# Import pandas package
import
pandas as pd
# Define a dictionary containing ICC rankings
rankings
=
{
'test'
: [
'India'
,
'South Africa'
,
'England'
,
'New Zealand'
,
'Australia'
],
'odi'
: [
'England'
,
'India'
,
'New Zealand'
,
'South Africa'
,
'Pakistan'
],
't20'
: [
'Pakistan'
,
'India'
,
'Australia'
,
'England'
,
'New Zealand'
]}
# Convert the dictionary into DataFrame
rankings_pd
=
pd.DataFrame(rankings)
# Before renaming the columns
print
(rankings_pd.columns)
rankings_pd.rename(columns
=
{
'test'
:
'TEST'
,
'odi'
:
'ODI'
,
't20'
:
'T20'
}, inplace
=
True
)
# After renaming the columns
print
(rankings_pd.columns)
Method #2: By assigning a list of new column names
The columns can also be renamed by directly assigning a list containing the new names to the columns
attribute of the dataframe object for which we want to rename the columns. The disadvantage with this method is that we need to provide new names for all the columns even if want to rename only some of the columns.
# Import pandas package
import
pandas as pd
# Define a dictionary containing ICC rankings
rankings
=
{
'test'
: [
'India'
,
'South Africa'
,
'England'
,
'New Zealand'
,
'Australia'
],
'odi'
: [
'England'
,
'India'
,
'New Zealand'
,
'South Africa'
,
'Pakistan'
],
't20'
: [
'Pakistan'
,
'India'
,
'Australia'
,
'England'
,
'New Zealand'
]}
# Convert the dictionary into DataFrame
rankings_pd
=
pd.DataFrame(rankings)
# Before renaming the columns
print
(rankings_pd.columns)
rankings_pd.columns
=
[
'TEST'
,
'ODI'
,
'T-20'
]
# After renaming the columns
print
(rankings_pd.columns)