How to assign a new column in a Pandas DataFrame?

How to assign a new column in a Pandas DataFrame?

How to assign a new column in a Pandas DataFrame?

This recipe helps you assign a new column in a Pandas DataFrame

In [1]:
## How to assign a new column in a Pandas DataFrame
def Kickstarter_Example_71():
    print(format('How to assign a new column in a Pandas DataFrame','*^82'))

    import warnings

    # load libraries
    import pandas as pd

    # Create empty dataframe
    df = pd.DataFrame()

    # Create a column
    df['StudentName'] = ['John', 'Steve', 'Sarah']

    # View dataframe
    print(); print(df)

    # Assign a new column to df called 'age' with a list of ages
    df = df.assign(Marks = [71, 82, 89])

    # View dataframe
    print(); print(df)

*****************How to assign a new column in a Pandas DataFrame*****************

0        John
1       Steve
2       Sarah

  StudentName  Marks
0        John     71
1       Steve     82
2       Sarah     89

Relevant Projects

Solving Multiple Classification use cases Using H2O
In this project, we are going to talk about H2O and functionality in terms of building Machine Learning models.

Data Science Project - Instacart Market Basket Analysis
Data Science Project - Build a recommendation engine which will predict the products to be purchased by an Instacart consumer again.

Data Science Project on Wine Quality Prediction in R
In this R data science project, we will explore wine dataset to assess red wine quality. The objective of this data science project is to explore which chemical properties will influence the quality of red wines.

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

Zillow’s Home Value Prediction (Zestimate)
Data Science Project in R -Build a machine learning algorithm to predict the future sale prices of homes.

Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

Human Activity Recognition Using Smartphones Data Set
In this deep learning project, you will build a classification system where to precisely identify human fitness activities.

Sequence Classification with LSTM RNN in Python with Keras
In this project, we are going to work on Sequence to Sequence Prediction using IMDB Movie Review Dataset​ using Keras in Python.

PySpark Tutorial - Learn to use Apache Spark with Python
PySpark Project-Get a handle on using Python with Spark through this hands-on data processing spark python tutorial.

Machine Learning or Predictive Models in IoT - Energy Prediction Use Case
In this machine learning and IoT project, we are going to test out the experimental data using various predictive models and train the models and break the energy usage.