
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python – Create a new column in a Pandas dataframe
To create a new column, we will use the already created column. At first, let us create a DataFrame and read our CSV −
dataFrame = pd.read_csv("C:\Users\amit_\Desktop\SalesRecords.csv")
Now, we will create a new column “New_Reg_Price” from the already created column “Reg_Price” and add 100 to each value, forming a new column −
dataFrame['New_Reg_Price'] = (dataFrame['Reg_Price'] + 100)
Example
Following is the code −
import pandas as pd # reading csv file dataFrame = pd.read_csv("C:\Users\amit_\Desktop\SalesRecords.csv") print("DataFrame...\n",dataFrame) # count the rows and columns in a DataFrame print("\nNumber of rows and column in our DataFrame = ",dataFrame.shape) dataFrame['New_Reg_Price'] = (dataFrame['Reg_Price'] + 100) print("Updated DataFrame with a new column...\n",dataFrame) print("\n[Updated] Number of rows and column in our DataFrame = ",dataFrame.shape)
Output
This will produce the following output −
DataFrame... Car Date_of_Purchase Reg_Price 0 BMW 10/10/2020 1000 1 Lexus 10/12/2020 750 2 Audi 10/17/2020 750 3 Jaguar 10/16/2020 1500 4 Mustang 10/19/2020 1100 5 Lamborghini 10/22/2020 1000 Number of rows and column in our DataFrame = (6, 3) Updated DataFrame with a new column ... Car Date_of_Purchase Reg_Price New_Reg_Price 0 BMW 10/10/2020 1000 1100 1 Lexus 10/12/2020 750 850 2 Audi 10/17/2020 750 850 3 Jaguar 10/16/2020 1500 1600 4 Mustang 10/19/2020 1100 1200 5 Lamborghini 10/22/2020 1000 1100 (Updated)Number of rows and column in our DataFrame = (6, 4)
- Related Articles
- Adding a new column to existing DataFrame in Pandas in Python
- Adding a new column to an existing DataFrame in Python Pandas
- Python - Add a new column with constant value to Pandas DataFrame
- Create a Pipeline and remove a column from DataFrame - Python Pandas
- Python Pandas - Create a DataFrame from original index but enforce a new index
- Adding new column to existing DataFrame in Pandas
- Python - Stacking a multi-level column in a Pandas DataFrame
- Python - Add a zero column to Pandas DataFrame
- Apply uppercase to a column in Pandas dataframe in Python
- Python - Calculate the variance of a column in a Pandas DataFrame
- Python - Add a prefix to column names in a Pandas DataFrame
- Create a Pivot Table as a DataFrame – Python Pandas
- Python Pandas – Display all the column names in a DataFrame
- Python Pandas – Remove numbers from string in a DataFrame column
- Python - How to select a column from a Pandas DataFrame

Advertisements