➠ Rename Column using select: select function can also be used to rename existing column, only downside is that user has specify all the dataframe columns(list can be accessed using df.columns) in select i.e columns which are required in final output. Example 2: Column db_type_test is not present in the given dataframe, therefore dataframe was returned as it is in the below example.ĭf_updated = df.withColumnRenamed("db_type_test","db_type_cd").Example 1: Column db_type was renamed to db_type_cd in the below example.ĭf_updated = df.withColumnRenamed("db_type","db_type_cd").This function takes 2 string parameters, 1st parameter is the name of the existing column and 2nd parameter is the new name of the column. WithColumnRenamed(existingColumnName, newColumnName) If the dataframe schema does not contain the given column then it will not fail and will return the same dataframe. ➠ Rename Column using withColumnRenamed: withColumnRenamed() function can be used on a dataframe to rename existing column. ➠ List all Columns: columns attribute can be used on a dataframe to return all the column names as a list. ĭf = ("file:///path_to_files/csv_file_with_duplicates.csv", header=True) Sample Data: Dataset used in the below examples can be downloaded from here. Rename all columns of a dataframe(suffix). Rename all columns of a dataframe(prefix).Print(Core_Dataframe.This tutorial will explain various approaches with examples on how to rename an existing column in a dataframe.īelow listed topics will be explained with examples on this page, click on item in the below list and it will take you to the respective section of the page: Print(" THE CORE DATAFRAME AFTER RENAME OPERATION ") Print(" THE CORE DATAFRAME BEFORE RENAME OPERATION ") to achieve this capability to flexibly travel over a dataframe the axis value is framed on below means ,) The value specified in this argument represents either a column position or a row position in the dataframe. This argument represents the column or the axis upon which the Rename() function needs to be applied on. So every rename values which are mentioned here will be applied to the column names of the dataframe. This is again an another alternative to the argument axis ( mapper, axis=1 ), Here columns as the name suggest it represents the columns of the dataframe. So every rename values which are mentioned here will be applied to the rows of the dataframe. This is an alternative to the argument axis ( mapper, axis=0 ), Here Index represents the rows of the dataframe. The description of this argument is explained below separately. The axis argument here mentions whether the change is for the column or the index. When using the Mapper argument it needs to be combined with the axis argument. The above dictionary when passed in the rename function it implies that the value ‘A’ in either the column or the index needs to be replaced as Header1 and similarly the column or index with value ‘B’ needs to be replaced with the new value ‘Header2’. The mapper argument usually takes values in the form of a dictionary. The mapper holds the values which needs to be replaced and their replacement values, So the old value and the corresponding new value which needs to be replaced will be specified here. Web development, programming languages, Software testing & others DataFrame.rename(self, mapper=None, index=None, columns=None, axis=None, copy=True, inplace=False, level=None, errors='ignore') Parameter & Description of Pandas DataFrame.rename()īelow are the parameters of Pandas DataFrame.rename() in Python: Parameter Start Your Free Software Development Course
0 Comments
Leave a Reply. |