Add Column to Pandas DataFrame with a Default Value

Add Column to Pandas DataFrame with a Default Value

To add a new column to a pandas DataFrame with a default value, you can simply assign the default value to the new column. Here's how you can do it:

import pandas as pd # Sample DataFrame df = pd.DataFrame({ 'A': [1, 2, 3], 'B': [4, 5, 6] }) print("Original DataFrame:") print(df) # Add new column 'C' with default value 10 df['C'] = 10 print("\nDataFrame after adding column 'C' with default value 10:") print(df) 

Output:

Original DataFrame: A B 0 1 4 1 2 5 2 3 6 DataFrame after adding column 'C' with default value 10: A B C 0 1 4 10 1 2 5 10 2 3 6 10 

The new column 'C' has been added to the DataFrame with a default value of 10 for each row.


More Tags

android-bottomsheetdialog http-get protocol-handler node-mysql spring-3 compiler-warnings navbar adjacency-matrix delta-lake tcsh

More Programming Guides

Other Guides

More Programming Examples