
- 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
How can a new column be created to a dataframe using the already present columns in Python?
Dataframe is a two dimensional data structure, where data is stored in a tabular format, in the form of rows and columns. It can be visualized as an SQL data table or an excel sheet representation.
It can be created using the following constructor −
pd.Dataframe(data, index, columns, dtype, copy)
We previously saw a method in which a new column was created as a Series data structure. This was indexed to the original dataframe and hence got added to the dataframe.
Let us use how we can create a column using the already present columns of the dataframe. This is useful when we need to perform some computation on the already present columns and store their result in a new column −
Example
import pandas as pd my_data = {'ab' : pd.Series([1, 8, 7], index=['a', 'b', 'c']), 'cd' : pd.Series([1, 2, 0, 9], index=['a', 'b', 'c', 'd']), 'ef' :pd.Series([56, 78, 32],index=['a','b','c'])} my_df = pd.DataFrame(my_data) print("The dataframe is :") print(my_df) my_df['gh'] = my_df['ab'] + my_df['ef'] print("After adding column 0 and 2 to the dataframe, :") print(my_df)
Output
The dataframe is : ab cd ef a 1.0 1 56.0 b 8.0 2 78.0 c 7.0 0 32.0 d NaN 9 NaN After adding column 0 and 2 to the dataframe, : ab cd ef gh a 1.0 1 56.0 57.0 b 8.0 2 78.0 86.0 c 7.0 0 32.0 39.0 d NaN 9 NaN NaN
Explanation
The required libraries are imported, and given alias names for ease of use.
Dictionary values consisting of key and value is created, wherein a value is actually a series data structure.
Multiple such dictionary values are created.
This dictionary is later passed as a parameter to the ‘Dataframe’ function present in the ‘pandas’ library
The dataframe is created by passing the dictionary as parameters to it.
A new column is indexed to the dataframe, and the 0th and 2nd column are added to create this new column.
The dataframe is printed on the console.
Note − The word ‘NaN’ refers to ‘Not a Number’, which means that specific [row,col] value doesn’t have any valid entry.
- Related Articles
- How can a new column be added to an existing dataframe in Python?
- How can a dataframe be created using a dictionary of Series in Python?
- How to create and fill a new column in an already created MySQL table?
- I want to create a new field in an already created document. How can this be done using MongoDB query?
- Explain how a dataframe structure can be created using list of dictionary values in Python?
- How to add a column using MySQL SELECT in an already created table?
- How can a column of a dataframe be deleted in Python?
- Create a Pipeline and remove a row from an already created DataFrame - Python Pandas
- Python – Create a new column in a Pandas dataframe
- 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
- How to delete a column of a dataframe using the ‘pop’ function in Python?
- How to append new rows to DataFrame using a Template In Python Pandas
- How to add NOT NULL constraint to an already created MySQL column?
