Applying Lambda functions to Pandas Dataframe
What are Dataframes?
If the data is stored in the form of rows and columns or two-dimensional data then it is generally called data frames in Pandas. In Pandas, we can add any function to any row or column with the help of the lambda function.
Using Dataframe.assign(), we can apply the lambda function to any single column.
In the above code, we have one dataframe where there are two columns with six rows where one column represents the name of the student, and another column represents the marks obtained. Using the assign() function, we have added a new column name percentage, and the values in this column are defined using lambda functions. We inserted the percentage by dividing the marks by 500 and multiplying by 100. we have applied the lambda function on the single column of marks obtained only.
Applying the lambda function to more than one column:
In the above code, we have three columns which represent some values. With the assign() function, we added one new column, and the values in the column are calculated using the lambda function by multiplying the values of all three columns.
So we can use multiple columns at a time using the lambda function.
As we have applied the lambda function on columns, we can apply the lambda function on rows also. With apply() function, we can apply the lambda function on a single row.
In the above code, we have a dataframe which contains six rows. We have assigned index names to each row. Using the lambda function, we are taking a square of the values whose index value is 'e'. So, if we run the code, the values in the fifth column are squared.
As we have applied the lambda function to a single row, we can apply it to multiple rows at a time. We will apply the function to implement the lambda function.
In the above code, we have applied the lambda function on multiple rows using DataFrame.apply() function. We squared the values of the rows whose index value is 'a', 'e' and 'g'. So in the output, we got these three rows as their square values.
As far as we have seen, we have applied the lambda function on either row or the columns. But we can also apply the lambda function on both rows and columns simultaneously using the function apply() and assign().
In the above code, we have applied a lambda function on rows and columns on a single dataframe. In the code, first, we have applied a lambda function on rows using the apply() function, which will square the values of some rows. Then we applied the lambda function on three columns using the assign() function, which will add a new column sum which contains the sum of all the columns.
So first, the values will be squared, and then they will be added to the fourth column.
Next TopicBox Plot in Python using Matplotlib
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week