How To Add Column In Pandas
Permit'due south come across how to add a new columns to an existing Pandas Dataframe.
Adding columns to a DataFrame is i of the almost crucial operations y'all have to perform while working on a project. It is required for several reasons such as adding new data which is relevant to the problem y'all are trying to solve or adding new features to improve the operation of the motorcar learning model.
In this article, you will encounter a number of methods to add columns of a pandas DataFrame followed past some practical tips.
Creating a DataFrame for demonstration
# Create the data every bit a dictionary import pandas as pd data_df = {'Proper noun': ['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
Using a List to add column in pandas
Create the new cavalcade as a list of values and directly assign information technology to the pandas DataFrame
# Create the new cavalcade as a listing new_col = ['Lee Kun-hee', 'Xu Zhijun', 'Tim Cook', 'Tony Chen', 'Shen Wei'] # Assign the listing to the DataFrame as a cavalcade df['Electric current Chairperson'] = new_col df
Using List unpacking to add cavalcade in pandas
List unpacking is the process of assigning multiple iterables (lists or tuples) to a listing of variables in a single statement.
You lot can employ the list unpacking operation to assign multiple columns at once.
# 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 list unpacking df['Electric current Chairperson'], df['Operating Arrangement Used'] = [new _col1, new _col2] df
Using a Lexicon to add column in pandas
You tin can add together the new column to a pandas DataFrame using a dictionary. The keys of the dictionary should exist the values of the existing column and the values to those keys will be the values of the new column.
Subsequently making the dictionary, laissez passer its values as the new cavalcade 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 lexicon as the values of the new column df['Current chairperson'] = col_dict.values() df
Using the DataFrame.insert() method
In other methods, the new column is created at the end of the dataframe. With the DataFrame.insert method, you can add a new column betwixt existing columns instead of adding them at the terminate of the pandas DataFrame.
- Syntax: pandas.DataFrame.insert(loc, column, value, allow_duplicates=False)
- 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 cavalcade. The integer value must exist between null to one less than the full number of columns.
- column: String or number or hashable object. We utilise this to specify the label of the cavalcade which will exist displayed for that column in the DataFrame.
- value: Integer or Series or array. Nosotros use it to specify the column we want to add.
- allow_duplicates Boolean (default: False). Information technology is used to specify that the new column, which is a duplicate of an existing column, should be added or non.
# 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 past specifying the index position, column name and the values of the cavalcade df.insert(loc=2, cavalcade='Current Chairperson', value=new_col) df
Using the DataFrame.assign() method
Permit us say you add together columns in pandas using the DataFrame.assign method. A new DataFrame will be created having the newly added columns to the original.
Always keep in heed that you cannot pass expressions (Strings, Integers,etc.) equally cavalcade 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 use this to specify the columns that are to be added.
- Returns:** pandas DataFrame
# Create a listing 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_2 = df.assign(Chairperson=new_col) df_2
Using the .loc() indexing method
We tin can use the row/column index labels in the loc indexing method to access rows and columns.
Nevertheless, you tin can also use this method for adding a new cavalcade to pandas DataFrames.
The start argument passed to this method is the row labels and the 2d statement is the column labels.
Y'all can utilize the colon symbol (:) to indicate that yous wish to admission all the rows and then pass the name of the new column every bit the 2d argument. So, you can assign a list of the values which will form the values of the new cavalcade.
# Create a listing 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
Practical Tips
- If y'all are creating a duplicate column from an existing column using whatever method other than the DataFrame.insert() method, brand sure that the column name of the duplicate column is unlike from the original otherwise the duplicate column will not exist created. For creating duplicate columns with the aforementioned name, use the DataFrame.insert method and set the value of the 'aloow_duplicate' parameter to True.
- While creating a new column using a lexicon, make sure to use the .values() method of the dictionary. If you apply this, the values of the dictionary will get passed equally the values of the new column. Otherwise, the keys of the dictionary will form the values of the new column.
- All the methods other than the DataFrame.insert() method volition add the columns at the end of the pandas DataFrame.
Test Your Cognition
Q1: To make a new cavalcade using the DataFrame.assign function, laissez passer the cavalcade name as a string and and then assign the list of values to the office. Truthful or False?
Want to go awesome in ML?
Hi! I am Selva, and I am excited yous are reading this!
Y'all can now go from a consummate beginner to a Data Science expert, with my end-to-terminate free Data Scientific discipline training.
No shifting between multiple books and courses. Hop on to the most constructive manner to becoming the skilful. (Includes downloadable notebooks, portfolio projects and exercises)
Beginning free with the starting time grade 'Foundations of Auto Learning' - a well rounded orientation of what the field of ML is all most.
Enroll to the Foundations of ML Class (Gratis)
Sold already? Commencement with the Complete ML Mastery Path
Answer:
Answer: False. Nosotros cannot utilize Keywords to make cavalcade names using the DataFrame.assign function.
Q2: What is the object returned when you add new columns using the DataFrame.assign part?
Respond:
Answer: The new columns are Nosotros will get a new DataFrame with new columns added to the original DataFrame.
Q3: Place the fault in the code and write the right lawmaking for the following:
df = df.assign("new_col_name") = new_col
Answer:
Answer: df = df.assign(new_col_name) = new_col
Q4: Assign the lists col_1, col_2, col_3 to a DataFrame df as new_col_1, new_col_2, new_col_3 using the listing unpacking office.
Answer:
Respond: df["new_col_1"], df["new_col_2"], df["new_col_3"] = [col_1, col_2, col_3]
Q5: Assign the lexicon data_dict to the DataFrame df as new_col.
Answer:
Answer: df['new_col'] = data_dict.values()
The article was contributed by Shreyansh B and Shri Varsheni.
How To Add Column In Pandas,
Source: https://www.machinelearningplus.com/pandas/pandas-add-column/
Posted by: huffalhas1974.blogspot.com
0 Response to "How To Add Column In Pandas"
Post a Comment