Add zero columns to Pandas Dataframe

Add zero columns to Pandas Dataframe

To add zero columns to a pandas DataFrame, you can simply assign a column with the value 0. Here's a step-by-step guide:

  • Import necessary libraries:
import pandas as pd 
  • Create a sample DataFrame:
data = { 'A': [1, 2, 3], 'B': [4, 5, 6] } df = pd.DataFrame(data) print(df) 

Output:

 A B 0 1 4 1 2 5 2 3 6 
  • Add zero columns:

Let's add two zero columns, named 'Z1' and 'Z2':

df['Z1'] = 0 df['Z2'] = 0 print(df) 

Output:

 A B Z1 Z2 0 1 4 0 0 1 2 5 0 0 2 3 6 0 0 

As shown, the columns 'Z1' and 'Z2' are added to the DataFrame, and they are filled with zeros.


More Tags

nuxt.js windows-installer beamer subset-sum arkit postgresql-9.1 instruction-encoding camera mailmerge binding

More Programming Guides

Other Guides

More Programming Examples