How to apply arithmatic operations on a Pandas DataFrame?
DATA MUNGING DATA CLEANING PYTHON MACHINE LEARNING RECIPES PANDAS CHEATSHEET     ALL TAGS

How to apply arithmatic operations on a Pandas DataFrame?

How to apply arithmatic operations on a Pandas DataFrame?

This recipe helps you apply arithmatic operations on a Pandas DataFrame

0

Recipe Objective

Have you ever tried to apply some operation in dataframe on string and numeric data.

So this is the recipe on how we can apply arithmatic operations on a Pandas DataFrame.

Step 1 - Import the library

import pandas as pd import numpy as np

We have only imported pandas and numpy which is needed.

Step 2 - Creating DataFrame

We have created a dictionary and passed it through pd.DataFrame to create a Dataframe raw_data = {"first_name": ["Sheldon", "Raj", "Leonard", "Howard", "Amy"], "age": [42, 38, 36, 41, 35], "Comedy_Score": [9, 7, 8, 8, 5], "Rating_Score": [25, 25, 49, 62, 70]} df = pd.DataFrame(raw_data, columns = ["first_name", "age", "Comedy_Score", "Rating_Score"]) print(df)

Step 3 - Applying Different Operation

We are making each letter of string in first name as capital. For this we creating a lambda function and by which are making every letter capital. capitalizer = lambda x: x.upper() print(df["first_name"].apply(capitalizer)) Now lets say we want to find the square root of the values in the dataframe. For this we have to remove the features which has strings so we have droped the feature first name. Now using applymap we have calculated square root of each values. df = df.drop("first_name", axis=1) print(df) print(df.applymap(np.sqrt)) Now lets say we want to multiply each value by 100. So for this we have created a function times100. In which we have used if else statements to return the same value if it is a string and multiply it by 100 if it is a numeric value. We have applied this function on every value of the dataframe. def times100(x): if type(x) is str: return x elif x: return 100 * x else: return print(df.applymap(times100)) So the output comes as

  first_name  age  Comedy_Score  Rating_Score
0    Sheldon   42             9            25
1        Raj   38             7            25
2    Leonard   36             8            49
3     Howard   41             8            62
4        Amy   35             5            70

0    SHELDON
1        RAJ
2    LEONARD
3     HOWARD
4        AMY
Name: first_name, dtype: object

0    SHELDON
1        RAJ
2    LEONARD
3     HOWARD
4        AMY
Name: first_name, dtype: object

   age  Comedy_Score  Rating_Score
0   42             9            25
1   38             7            25
2   36             8            49
3   41             8            62
4   35             5            70

        age  Comedy_Score  Rating_Score
0  6.480741      3.000000      5.000000
1  6.164414      2.645751      5.000000
2  6.000000      2.828427      7.000000
3  6.403124      2.828427      7.874008
4  5.916080      2.236068      8.366600

    age  Comedy_Score  Rating_Score
0  4200           900          2500
1  3800           700          2500
2  3600           800          4900
3  4100           800          6200
4  3500           500          7000
‚Äč

Relevant Projects

Predict Macro Economic Trends using Kaggle Financial Dataset
In this machine learning project, you will uncover the predictive value in an uncertain world by using various artificial intelligence, machine learning, advanced regression and feature transformation techniques.

Predict Credit Default | Give Me Some Credit Kaggle
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.

Forecast Inventory demand using historical sales data in R
In this machine learning project, you will develop a machine learning model to accurately forecast inventory demand based on historical sales data.

Data Science Project-TalkingData AdTracking Fraud Detection
Machine Learning Project in R-Detect fraudulent click traffic for mobile app ads using R data science programming language.

Predict Employee Computer Access Needs in Python
Data Science Project in Python- Given his or her job role, predict employee access needs using amazon employee database.

German Credit Dataset Analysis to Classify Loan Applications
In this data science project, you will work with German credit dataset using classification techniques like Decision Tree, Neural Networks etc to classify loan applications using R.

Mercari Price Suggestion Challenge Data Science Project
Data Science Project in Python- Build a machine learning algorithm that automatically suggests the right product prices.

Identifying Product Bundles from Sales Data Using R Language
In this data science project in R, we are going to talk about subjective segmentation which is a clustering technique to find out product bundles in sales data.

Resume parsing with Machine learning - NLP with Python OCR and Spacy
In this machine learning resume parser example we use the popular Spacy NLP python library for OCR and text classification.