C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Pandas DataFrame.transformWe can define Pandas DataFrame as a two-dimensional size-mutable, heterogeneous tabular data structure with some labeled axes (rows and columns). Performing the arithmetic operations will align both row and column labels. It can be considered as a dict-like container for Series objects. The main task of Pandas DataFrame.transform() function is to self produce a DataFrame with its transformed values and it has the same axis length as self. Syntax:DataFrame.transform(func, axis=0, *args, **kwargs) Parameters :func : It is a function that is used for transforming the data. axis : Refers to 0 or 'index', 1 or 'columns', default value 0. *args: It is a positional arguments that is to be passed to a func. **kwargs : It is a keyword arguments that is to be passed to a func. Returns:It returns the DataFrame that must have same length as self. Example 1 : Use DataFrame.transform() function to add 10 to each element in the dataframe. # importing pandas as pd importpandas as pd # Creating the DataFrame info =pd.DataFrame({"P":[8, 2, 9, None, 3], "Q":[4, 14, 12, 22, None], "R":[2, 5, 7, 16, 13], "S":[16, 10, None, 19, 18]}) # Create the index index_ =['A_Row', 'B_Row', 'C_Row', 'D_Row', 'E_Row'] # Set the index info.index =index_ # Print the DataFrame print(info) Output: P Q R S A_Row 8.0 4.0 2.0 16.0 B_Row 2.0 14.0 5.0 10.0 C_Row 9.0 12.0 7.0 NaN D_RowNaN 22.0 16.0 19.0 E_Row 3.0NaN 13.0 18.0 Example 2 : Use DataFrame.transform() function to find the square root and the result of euler's number raised to each element of the dataframe. # importing pandas as pd importpandas as pd # Creating the DataFrame info =pd.DataFrame({"P":[8, 2, 9, None, 3], "Q":[4, 14, 12, 22, None], "R":[2, 5, 7, 16, 13], "S":[16, 10, None, 19, 18]}) # Create the index index_ =['A_Row', 'B_Row', 'C_Row', 'D_Row', 'E_Row'] # Set the index info.index =index_ # Print the DataFrame print(info) Output: P Q R S A_Row 88.0 14.0 12.0 16.0 B_Row 12.0 14.0 15.0 10.0 C_Row 19.0 22.0 17.0 NaN D_RowNaN 21.0 16.0 19.0 E_Row 13.0NaN 13.0 18.0
Next TopicDataFrame.transpose()
|