TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Pandas DataFrame.transform

Pandas DataFrame.transform with What is Python Pandas, Reading Multiple Files, Null values, Multiple index, Application, Application Basics, Resampling, Plotting the data, Moving windows functions, Series, Read the file, Data operations, Filter Data etc.

<< Back to PANDAS

Pandas DataFrame.transform

We 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





Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf