Coding Ref

How to add an empty column to a Pandas DataFrame

How to add an empty column to a Pandas DataFrame

To add an empty column to a Pandas DataFrame, you can use the assign method.

This method allows you to add new columns to a DataFrame by specifying the column names and values as keyword arguments.

For example, consider the following DataFrame:

main.py
import pandas as pd

df = pd.DataFrame({
    'A': [1, 2, 3, 4, 5],
    'B': [10, 20, 30, 40, 50],
    'C': [100, 200, 300, 400, 500]
})

This DataFrame has three columns A, B, and C, with five rows of data. To add an empty column to this DataFrame, you could do the following:

main.py
import pandas as pd

df = pd.DataFrame({
    'A': [1, 2, 3, 4, 5],
    'B': [10, 20, 30, 40, 50],
    'C': [100, 200, 300, 400, 500]
})

# Add an empty column named "D"
df_with_empty_col = df.assign(D=[])

# Print the resulting DataFrame
print(df_with_empty_col)

In the code above, the assign method is applied to the DataFrame, and the column name D and an empty list are specified as keyword arguments.

This tells the assign method to add a new column named D to the DataFrame with no values.

The result is a new DataFrame object that contains the original columns A, B, and C, plus a new empty column named D.

Add a new column with specified values

You can also specify values for the new column when using the assign method. For example, if you wanted to add a column named D with the values 1, 2, 3, 4, 5, you could do the following:

main.py
import pandas as pd

df = pd.DataFrame({
    'A': [1, 2, 3, 4, 5],
    'B': [10, 20, 30, 40, 50],
    'C': [100, 200, 300, 400, 500]
})

# Add a column named "D" with the values 1, 2, 3, 4, 5
df_with_col = df.assign(D=[1, 2, 3, 4, 5])

# Print the resulting DataFrame
print(df_with_col)

In the code above, the assign method is applied to the DataFrame, and the column name D and a list of values are specified as keyword arguments.

This tells the assign method to add a new column named D to the DataFrame with the specified values.

The result is a new DataFrame object that contains the original columns A, B, and C, plus a new column named D with the values 1, 2, 3, 4, 5.

Add multiple columns to a Pandas DataFrame

You can also use the assign method to add multiple columns to a DataFrame.

Here is an example:

main.py
import pandas as pd

# create a sample DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

# add two new columns, "C" and "D", to the DataFrame
df = df.assign(C=df['A'] * 2, D=df['B'] * 3)

# print the resulting DataFrame
print(df)

This code will output the following DataFrame:

output
   A  B  C   D
0  1  4  2  12
1  2  5  4  15
2  3  6  6  18

As you can see, the assign() method was used to add two new columns, C and D, to the DataFrame. The values for these columns were calculated using values from the original DataFrame.

You can use the assign() method to add as many columns as you need to the DataFrame, and you can use any valid pandas expressions to calculate the values for the new columns.

You'll also like

Related tutorials curated for you

    How to GroupBy Index in Pandas

    How to sort a series in Pandas

    Pandas read SQL

    How to fix: AttributeError module 'pandas' has no attribute 'dataframe'

    How to normalize a column in Pandas

    How to use applymap() in Pandas

    How to use where() in Pandas

    What is nlargest() in Pandas?

    How to shuffle data in Pandas

    How to select multiple columns in Pandas

    How to convert a series to a NumPy array in Pandas

    How to calculate the variance in Pandas DataFrame