Pandas DataFrame.rename()The main task of the Pandas rename() function is to rename any index, column, or row. This method is useful for renaming some selected columns because we have to specify the information only for those columns that we want to rename. It mainly alters the axes labels based on some of the mapping (dict or Series) or the arbitrary function. The function must be unique and should range from 1 to -1. The labels will be left, if it is not contained in a dict or Series. If you list some extra labels, it will throw an error. Syntax:Parameters:
Returns:It returns the DataFrame with renamed axis labels. Example 1: The below example renames a single column: Output: name age language 0 Parker 38 Java 1 Smith 47 Python 2 William 44 JavaScript 3 Robert 34 Python After modifying first column: Index(['Name', 'age', 'language'], dtype='object') Example2: The below example renames the multiple columns: Output: name age language 0 Parker 38 Java 1 Smith 47 Python 2 William 44 JavaScript 3 Robert 34 Python Index(['Name', 'Age', 'Language'], dtype='object') Example3: The below example renames indexes of a particular column: Output: DataFrame: Name Emp_ID Language 0 Smith 101 Python 1 Parker 102 Java 2 William 103 JavaScript Renamed Indexes: Name Emp_ID Language #0 Smith 101 Python #1 Parker 102 Java #2 William 103 JavaScript
Next TopicDataFrame.sample()
|