Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
Python - Add a zero column to Pandas DataFrame
To add a zero column to a Pandas DataFrame, use the square bracket and set it to 0. At first, import te required library −
import pandas as pd
Create a DataFrame with 3 columns −
dataFrame = pd.DataFrame(
{
"Student": ['Jack', 'Robin', 'Ted', 'Marc', 'Scarlett', 'Kat', 'John'],"Result": ['Pass', 'Fail', 'Pass', 'Fail', 'Pass', 'Pass', 'Pass'],"Roll Number": [ 5, 10, 3, 8, 2, 9, 6]
}
)
Create a new column with zero entries −
dataFrame['ZeroColumn'] = 0
Example
Following is the code −
import pandas as pd
# Create DataFrame
dataFrame = pd.DataFrame(
{
"Student": ['Jack', 'Robin', 'Ted', 'Marc', 'Scarlett', 'Kat', 'John'],"Result": ['Pass', 'Fail', 'Pass', 'Fail', 'Pass', 'Pass', 'Pass'],"Roll Number": [ 5, 10, 3, 8, 2, 9, 6]
}
)
print"DataFrame ...\n",dataFrame
dataFrame['ZeroColumn'] = 0
print"\nUpdated DataFrame ...\n",dataFrame
Output
This will produce the following output −
DataFrame ... Result Roll Number Student 0 Pass 5 Jack 1 Fail 10 Robin 2 Pass 3 Ted 3 Fail 8 Marc 4 Pass 2 Scarlett 5 Pass 9 Kat 6 Pass 6 John Updated DataFrame ... Result Roll Number Student ZeroColumn 0 Pass 5 Jack 0 1 Fail 10 Robin 0 2 Pass 3 Ted 0 3 Fail 8 Marc 0 4 Pass 2 Scarlett 0 5 Pass 9 Kat 0 6 Pass 6 John 0
Advertisements
