- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Adding a new column to an existing DataFrame in Python Pandas
To add a new column to an existing DataFrame we can just make a column and assign values to it that is equal to the same number of rows with other columns.
Steps
Create a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.
Print the input DataFrame.
Create a new column, a, and assign values to this column.
Print the DataFrame, df.
Example
import pandas as pd df = pd.DataFrame( { "x": [5, 2, 1, 9], "y": [4, 1, 5, 10], "z": [4, 1, 5, 0] } ) print "Input DataFrame is:
", df df['a'] = [2, 4, 1, 0] print "After adding new column:
", df
Output
Input DataFrame is: x y z 0 5 4 4 1 2 1 1 2 1 5 5 3 9 10 0 After adding new column: x y z a 0 5 4 4 2 1 2 1 1 4 2 1 5 5 1 3 9 10 0 0
- Related Articles
- Adding a new column to existing DataFrame in Pandas in Python
- Adding new column to existing DataFrame in Pandas
- How can a new column be added to an existing dataframe in Python?
- Adding new enum column to an existing MySQL table?
- Python – Create a new column in a Pandas dataframe
- Adding a new NOT NULL column to an existing table with records
- Python - Add a new column with constant value to Pandas DataFrame
- Append list of dictionaries to an existing Pandas DataFrame in Python
- Python - Add a zero column to Pandas DataFrame
- Apply uppercase to a column in Pandas dataframe in Python
- Python - Add a prefix to column names in a Pandas DataFrame
- Python - How to select a column from a Pandas DataFrame
- Adding characters in values for an existing int column in MySQL?
- Python - Move a column to the first position in Pandas DataFrame?
- Python - Stacking a multi-level column in a Pandas DataFrame

Advertisements