banner



How To Add Column To Data Frame

Allow's see how to add a new columns to an existing Pandas Dataframe.

Calculation columns to a DataFrame is one of the about crucial operations you have to perform while working on a project. It is required for several reasons such as adding new information which is relevant to the problem yous are trying to solve or adding new features to amend the performance of the machine learning model.

In this article, you volition run across a number of methods to add columns of a pandas DataFrame followed past some practical tips.

Creating a DataFrame for demonstration

                      # Create the data as a lexicon            import            pandas            as            pd data_df = {'Name': ['Samsung',            'Huawei',            'Apple',            'Oppo',            'Vivo'],            'Founder': ['Lee Byung-Chul',            'Ren Zhengfei',            'Steve Jobs',            'Tony Chen',            'Shen Wei'],            'Year Founded in': [1938,            1987,            1976,            2004,            2009]}            # Create the DataFrame            df = pd.DataFrame(data_df) df                  
Basic Dataframe

Using a List to add together cavalcade in pandas

Create the new cavalcade as a list of values and straight assign it to the pandas DataFrame

                      # Create the new column as a listing            new_col            = ['Lee Kun-hee',            'Xu Zhijun',            'Tim Melt',            'Tony Chen',            'Shen Wei']            # Assign the list to the DataFrame as a column            df['Current Chairperson'] = new_col df                  
Using List to add new column in pandas

Using List unpacking to add together cavalcade in pandas

List unpacking is the process of assigning multiple iterables (lists or tuples) to a list of variables in a unmarried statement.

You can use the list unpacking performance to assign multiple columns at in one case.

                      # Create the lists            new            _col1            = ['Lee Kun-hee',            'Xu Zhijun',            'Tim Cook',            'Tony Chen',            'Shen Wei']            new            _col2            = ['Android',            'HarmonyOS',            'macOS',            'ColorOS',            'FuntouchOS']            # Assign both the lists to the DataFrame using listing unpacking            df['Current Chairperson'], df['Operating System Used'] = [new            _col1,            new            _col2] df                  
Using List Unpacking to add new column in pandas

Using a Dictionary to add column in pandas

You lot can add together the new column to a pandas DataFrame using a dictionary. The keys of the lexicon should be the values of the existing column and the values to those keys will be the values of the new column.
After making the lexicon, pass its values as the new column to the DataFrame.

                      # Create the dictionary containing the data of the new column            col_dict = {'Samsung':            'Lee kun-hee',            'Huawei':            'Xu Zhijun',            'Apple tree':            'Tim Cook',            'Oppo':            'Tony Chen',            'Vivo':            'Shen Wei'}            # Assign the values of the dictionary equally the values of the new column            df['Current chairperson'] = col_dict.values() df                  
Using Dictionary to add new column in pandas

Using the DataFrame.insert() method

In other methods, the new column is created at the finish of the dataframe. With the DataFrame.insert method, you can add a new column between existing columns instead of adding them at the end of the pandas DataFrame.

  • Syntax: pandas.DataFrame.insert(loc, cavalcade, value, allow_duplicates=Fake)
  • Purpose: To add together a new column to a pandas DataFrame at a user-specified location.
  • Parameters:
    • loc: Int. It is used to specify the integer-based location for inserting the new column. The integer value must be between nil to one less than the total number of columns.
    • column: String or number or hashable object. We use this to specify the label of the column which volition be displayed for that column in the DataFrame.
    • value: Integer or Serial or array. We use it to specify the column we desire to add.
    • allow_duplicates Boolean (default: Simulated). Information technology is used to specify that the new column, which is a duplicate of an existing column, should exist added or not.
                      # Create a list which contains the values of the new column            new_col = ['Lee Kun-hee',            'Xu Zhijun',            'Tim Cook',            'Tony Chen',            'Shen Wei']            # Assign the column by specifying the index position, cavalcade proper name and the values of the column            df.insert(loc=two, column='Current Chairperson',            value=new_col) df                  
Using insert method

Using the DataFrame.assign() method

Permit us say y'all add together columns in pandas using the DataFrame.assign method. A new DataFrame will exist created having the newly added columns to the original.

Always keep in mind that you cannot pass expressions (Strings, Integers,etc.) as column names using this method.

  • Syntax: pandas.DataFrame.assign( kwargs)
  • Purpose: To return a new DataFrame object having the new columns along with the columns of the original DataFrame.
  • Parameters: kwargs: Nosotros apply this to specify the columns that are to be added.
  • Returns:** pandas DataFrame
                      # Create a list which contains the values of the new cavalcade            new_col            = ['Lee Kun-hee',            'Xu Zhijun',            'Tim Cook',            'Tony Chen',            'Shen Wei']            # Assign the column to the DataFrame            df_2 = df.assign(Chairperson=new_col) df_2                  
Using allign method

Using the .loc() indexing method

Nosotros can apply the row/column index labels in the loc indexing method to access rows and columns.
However, you can also apply this method for adding a new cavalcade to pandas DataFrames.

The first argument passed to this method is the row labels and the second argument is the cavalcade labels.

Yous tin use the colon symbol (:) to indicate that you wish to admission all the rows and then pass the name of the new column as the second argument. Then, y'all can assign a list of the values which will class the values of the new column.

                      # Create a list which contains the values of the new column            new_col            = ['Lee Kun-hee',            'Xu Zhijun',            'Tim Cook',            'Tony Chen',            'Shen Wei']            # Assign the column to the DataFrame            df.loc[:,            'Current Chairperson'] = new_col df                  
Using .loc method

Practical Tips

  • If you are creating a duplicate column from an existing column using any method other than the DataFrame.insert() method, brand sure that the column name of the duplicate cavalcade is different from the original otherwise the duplicate cavalcade will non be created. For creating duplicate columns with the same name, use the DataFrame.insert method and prepare the value of the 'aloow_duplicate' parameter to True.
  • While creating a new cavalcade using a dictionary, make sure to utilize the .values() method of the dictionary. If you use this, the values of the dictionary will get passed as the values of the new cavalcade. Otherwise, the keys of the lexicon will form the values of the new column.
  • All the methods other than the DataFrame.insert() method will add the columns at the end of the pandas DataFrame.

Examination Your Knowledge

Q1: To make a new column using the DataFrame.assign role, laissez passer the column name as a string and and so assign the list of values to the part. True or False?

Want to go awesome in ML?

Hi! I am Selva, and I am excited you are reading this!
You can now become from a complete beginner to a Data Scientific discipline expert, with my end-to-end costless Data Science training.
No shifting between multiple books and courses. Hop on to the most effective way to condign the expert. (Includes downloadable notebooks, portfolio projects and exercises)

Start gratuitous with the start class 'Foundations of Machine Learning' - a well rounded orientation of what the field of ML is all about.

Enroll to the Foundations of ML Course (FREE)

Sold already? Start with the Complete ML Mastery Path

Answer:

Answer: False. We cannot use Keywords to brand cavalcade names using the DataFrame.assign function.

Q2: What is the object returned when you add new columns using the DataFrame.assign role?

Answer:

Respond: The new columns are We volition get a new DataFrame with new columns added to the original DataFrame.

Q3: Place the error in the code and write the correct lawmaking for the following:

df = df.assign("new_col_name") = new_col

Answer:

Respond: df = df.assign(new_col_name) = new_col

Q4: Assign the lists col_1, col_2, col_3 to a DataFrame df every bit new_col_1, new_col_2, new_col_3 using the list unpacking function.

Answer:

Respond: df["new_col_1"], df["new_col_2"], df["new_col_3"] = [col_1, col_2, col_3]

Q5: Assign the dictionary data_dict to the DataFrame df as new_col.

Answer:

Respond: df['new_col'] = data_dict.values()

The article was contributed by Shreyansh B and Shri Varsheni.

How To Add Column To Data Frame,

Source: https://www.machinelearningplus.com/pandas/pandas-add-column/

Posted by: kentunclefor.blogspot.com

0 Response to "How To Add Column To Data Frame"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel