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

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

Machine Learning project for Retail Price Optimization
In this machine learning pricing project, we implement a retail price optimization algorithm using regression trees. This is one of the first steps to building a dynamic pricing model.

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.

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

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.

Learn to prepare data for your next machine learning project
Text data requires special preparation before you can start using it for any machine learning project.In this ML project, you will learn about applying Machine Learning models to create classifiers and learn how to make sense of textual data.

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.

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.

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.