How to Add a Column to a DataFrame in Python Pandas

In this Python Pandas tutorial, we will learn how to add a column to a dataframe in Python Pandas. All the dataset used is either self-created or downloaded from Kaggle. Also, we have covered these topics.

  • Add a Column to a DataFrame in Python Pandas
  • Append a Column to a DataFrame in Pandas
  • Add a Column to a DataFrame in Python With the Same Value
  • Adding Calculated Columns to a DataFrame in Pandas
  • Add a Column Name to a DataFrame Pandas
  • Add An Empty Column to a DataFrame in Pandas
  • Adding a Blank Column to a DataFrame in Python
  • Add a Column to a Pandas DataFrame Based on an if-else Condition
  • Add a Column to a DataFrame From Another DataFrame Pandas

Add a Column to a DataFrame in Python Pandas

In this section, we will learn how to add a column to a dataframe in Python Pandas.

  • While working with the dataset in Python Pandas creation and deletion of column is an active process. New columns with new data are added and columns that are not required are removed.
  • Columns can be added in three ways in an exisiting dataframe.
    • dataframe.assign()
    • dataframe.insert()
    • dataframe[‘new_column’] = value
  • In dataframe.assign() method we have to pass the name of new column and it’s value(s). If only one value is provided then it will be assigned to entire dataset if list of values are provided then it will be assigned accordingly.
  • In dataframe.insert() method, user has to provide location or position where to insert the column, column name, value(s) for the column, boolean value for if duplicates are allowed or not.
  • third option is self explanatory also you can read the comments in the Jupyter notebook to understand every step.
  • Please note if you are providing list of values then total values must be equal to number of rows.

Read: Convert Pandas DataFrame to NumPy Array

Append a column to a DataFrame in Pandas

In this section, we will learn how to append a column to a dataframe in Python Pandas.

  • Append method is used to rows of other dataframe to existing dataframe.
  • Using dataframe.append() method in Python we can append the rows of other dataframe to an exisitng one.
  • If there is any extra column then new column is created with that name.
  • All the missing data is represented with ‘NaN’, anywhere the information is not available there NaN is filled.
  • Set ignore_index=True, so that index value of new column is continued after the previous row. by default this is set to false, that means what ever the index value new data has in previous dataframe that will be continued here.
  • Here is the implementation of append method in Python Pandas on Jupyter Notebook.

Read: How to Find Duplicates in Python DataFrame

Add a Column to a DataFrame in Python With the Same Value

In this section, we will learn how to add a column to a dataframe in Python with the same value.

  • In a dataset, at times Engineer has to set the same value for the particular column. For Example if the dataset is related to women only then Gender column could have female value only.
  • In our previous section, we have learned how to add a column to dataframe in pandas.
  • So to provide same value simply provide one value without a list. In this way same value will be assigned to all the rows of the column.
  • In our example, we have added new column with the name ‘Rating’ and we have assinged 5 to all the rows.

Read: Add row to Dataframe Python Pandas

Adding Calculated Columns to a DataFrame in Pandas

In this section, we will learn how to add calculated columns to a dataframe in pandas.

  • While working with the dataset, data engineers perform calculations on few columns and the result is stored in a new column.
  • This could be a mean, median or mode column. At times they create other columns as well like current salary and incremented salary wherein new salary is incremented by some percentage over the previous salary.
  • In our example, we will increament the peak_player value by 10% and will show show the result in a new column with the name new_peak_players.
  • Here is the implementation on jupyter notebook.

Read: Check If DataFrame is Empty in Python Pandas

Add a Column Name to a DataFrame Pandas

In this section, we will learn how to add column names to a dataframe pandas.

  • While working with the dataset in Python Pandas creation and deletion of column is an active process. New columns with new data are added and columns that are not required are removed.
  • Columns can be added in three ways in an exisiting dataframe.
    • dataframe.assign()
    • dataframe.insert()
    • dataframe[‘new_column’] = value
  • In dataframe.assign() method we have to pass the name of new column and it’s value(s). If only one value is provided then it will be assigned to entire dataset if list of values are provided then it will be assigned accordingly.
  • In dataframe.insert() method, user has to provide location or position where to insert the column, column name, value(s) for the column, boolean value for if duplicates are allowed or not.
  • third option is self explanatory also you can read the comments in the Jupyter notebook to understand every step.
  • Please note if you are providing list of values then total values must be equal to number of rows.

Read: Python Pandas replace multiple values

Add An Empty Column to a DataFrame in Pandas

In this section, we will learn how to add an empty column to a dataframe in Python Pandas.

  • Empty data can also be considered as a missing data or NaN values.
  • Using np.nan option from Numpy we can add column with an empty values.
  • In our example, you can notice that we have added a new column with the name Rating and it has missing values or NaN values.
  • Here is the implementation on Jupyter Notebook.

Read: How to Set Column as Index in Python Pandas

Adding a Blank Column to a DataFrame in Python

In this section, we will learn how to add a blank column to a dataframe in Python Pandas.

  • Adding a blank column can be referred to adding missing values (nan) or empty string.
  • We have already covered adding on missing values in a dataframe in our previous section.
  • In this section, we will learn how to add a column with blank values ot empty string.
  • to add an empty string simply pass ' ' or " " in place on value. this will add blank value in a dataframe.
  • Here is the implementation on Jupyter Notebook.

Read: Python DataFrame to CSV

Add a Column to a Pandas DataFrame Based on an if-else Condition

In this section, we will learn how to add a column to a pandas dataframe based on an if-else condition.

  • If-else condition is used to create a lader of statements.
  • While working with the datasets, engnieers have to put a condition to filter or clean the data based upon some condition.
  • For example dividing the dataset into two parts based upon gender.
  • In our example, we will create new column with the name state. If the valueof peak_player is less than certain amount that a new column will be created with state set to 1 otherwise it will be set to 0.
  • Here is the implementation on Jupyter Notebook.

Read: Get index Pandas Python

Add a Column to a DataFrame From Another DataFrame Pandas

In this section, we will learn how to add a column to a dataframe from another dataframe in Python Pandas.

  • To demonstrate this we have created two dataframes in our example on Jupyter Notebook.
  • using a code, df1['Address'] = df2['City'] we have added new column in first dataframe with the name address and we have put the city name from the second dataframe to first dataframe.
  • Here is the implementation on Jupyter Notebook.

Also, read the related articles.

In this tutorial, we have learned how to add a Column to a DataFrame in Python Pandas. Also, we have covered these topics.

  • Add a Column to a DataFrame in Python Pandas
  • Append a Column to a DataFrame in Pandas
  • Add a Column to a DataFrame in Python Pandas With the Same Value
  • Adding Calculated Columns to a DataFrame in Pandas
  • Add a Column Name to a DataFrame Pandas
  • Add An Empty Column to a DataFrame in Pandas
  • Adding a Blank Column to a DataFrame in Python
  • Add a Column to a Pandas DataFrame Based on an if-else Condition
  • Add a Column to a DataFrame From Another DataFrame Pandas