How To Add a Column to a Data Frame in Pandas (Python)

How To Add a Column to a Data Frame in Pandas (Python)

Channel:
Subscribers:
53,100
Published on ● Video Link: https://www.youtube.com/watch?v=6WmNmAdX5wU



Duration: 3:27
8,850 views
134


This video shows 3 ways to add new columns to Pandas Data Frames in Python. Adding new columns is a basic data manipulation task that is a common part of the data cleaning and wrangling process.

If you find this video useful, like, share and subscribe to support the channel!
► Subscribe: https://www.youtube.com/c/DataDaft?sub_confirmation=1


Code used in this Python Code Clip:

import pandas as pd

import statsmodels.api as sm #(To access mtcars dataset)
mtcars = sm.datasets.get_rdataset("mtcars", "datasets", cache=True).data

mtcars.head()

# Method #1: Direct Column Assignment

mtcars["new_1"] = mtcars.mpg > 20

mtcars.head()

# Method #2: Use .insert()

mtcars.insert(2, # Where to insert
"new_2", # Name of new column
mtcars.mpg > 25) # What to insert

mtcars.head()

# Method #3: Use .assign()

mtcars = mtcars.assign(new_3 = mtcars.mpg > 15, # New column
new_4 = mtcars.mpg > 30) # Another new column

mtcars.head()


* Note: YouTube does not allow greater than or less than symbols in the text description, so the code above will not be exactly the same as the code shown in the video! I will use Unicode large < and > symbols in place of the standard sized ones. .


⭐ Kite is a free AI-powered coding assistant that integrates with popular editors and IDEs to give you smart code completions and docs while you’re typing. It is a cool application of machine learning that can also help you code faster! Check it out here: https://www.kite.com/get-kite/?utm_medium=referral&utm_source=youtube&utm_campaign=datadaft&utm_content=description-only







Tags:
add a column to a data frame in python
add a column to a data frame in pandas
pandas add column
pandas add columns
python data frames
pandas data frames
add a column to a dataframe in pandas
add a column to a dataframe in python
python add column
pandas basics
add columns in pandas
add a column in pandas
create new columns in pandas
create new column in pandas
pandas dataframes
learn pandas
python data science
learn python
pandas
python
python dataframes