logo

Comment renommer des colonnes dans Pandas DataFrame

Étant donné un Pandas DataFrame, voyons comment renommer des colonnes dans Pandas avec des exemples. Ici, nous discuterons de 5 façons différentes de renommer les noms de colonnes dans pandas DataFrame.

Comment renommer des colonnes dans Pandas DataFrame

Méthode 1 : Utilisation de la fonction rename()

Une façon de renommer les colonnes dans un Pandas Dataframe consiste à utiliser la fonction rename(). Cette méthode est très utile lorsque nous devons renommer certaines colonnes sélectionnées car nous devons spécifier des informations uniquement pour les colonnes qui doivent être renommées.



Exemple 1: Renommer un seule colonne .

Python
# 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('
After modifying first column:
', rankings_pd.columns)>

Sortir:



Exemple 2 : Renommer plusieurs colonnes .

Python
# 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)>

Sortir:

Méthode 2 : En attribuant une liste de nouveaux noms de colonnes

Les colonnes peuvent également être renommées en attribuant directement une liste contenant les nouveaux noms à l'attribut columns de l'objet Dataframe pour lequel on souhaite renommer les colonnes. L'inconvénient de cette méthode est que nous devons fournir de nouveaux noms pour toutes les colonnes même si nous ne souhaitons renommer que certaines colonnes.



Python
# 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)>

Sortir:

Méthode 3 : Renommer les noms de colonnes à l’aide de la fonction DataFrame set_axis()

Dans cet exemple, nous renommerons le nom de la colonne à l'aide de la fonction set_axis, nous passerons le nouveau nom de la colonne et l'axe qui doit être remplacé par un nouveau nom dans la colonne en paramètre.

Python
# 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.set_axis(['A', 'B', 'C'], axis='columns') # After renaming the columns print(rankings_pd.columns) rankings_pd.head()>

Sortir:

éducation shloka mehta

Méthode 4 : Renommer les noms de colonnes à l'aide des fonctions DataFrame add_prefix() et add_suffix()

Dans cet exemple, nous renommerons le nom de la colonne à l'aide des fonctions add_Sufix et add_Prefix, nous transmettrons le préfixe et le suffixe qui doivent être ajoutés au prénom et au nom du nom de la colonne.

Python
# 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 = rankings_pd.add_prefix('col_') rankings_pd = rankings_pd.add_suffix('_1') # After renaming the columns rankings_pd.head()>

Sortir:

 col_test_1 col_odi_1 col_t20_1 0 India England Pakistan 1 South Africa India India 2 England New Zealand Australia 3 New Zealand South Africa England 4 Australia Pakistan New Zealand>

Méthode 5 : Remplacer les textes spécifiques des noms de colonnes à l'aide de la fonction Dataframe.columns.str.replace

Dans cet exemple, nous renommerons le nom de la colonne à l'aide de la fonction replace, nous passerons l'ancien nom avec le nouveau nom en paramètre de la colonne.

Python
# 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) # df = rankings_pd rankings_pd.columns = rankings_pd.columns.str.replace('test', 'Col_TEST') rankings_pd.columns = rankings_pd.columns.str.replace('odi', 'Col_ODI') rankings_pd.columns = rankings_pd.columns.str.replace('t20', 'Col_T20') rankings_pd.head()>

Sortir: