Create a column using for loop in Pandas Dataframe Last Updated : 09 Jun, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Adding a new column in an already created dataframe is quite easy. Adding a new column is actually required to process the data of dataframe created earlier. For that purpose, we can process the existing data and make a separate column to store the data. The simplest way to add a new column along with data is by creating a new column and assigning new values to it. For example: Python3 import pandas as pd # Creating new dataframe initial_data = {'First_name': ['Ram', 'Mohan', 'Tina', 'Jeetu', 'Meera'], 'Last_name': ['Kumar', 'Sharma', 'Ali', 'Gandhi', 'Kumari'], 'Marks': [12, 52, 36, 85, 23] } df = pd.DataFrame(initial_data, columns = ['First_name', 'Last_name', 'Marks']) df['Results']=['Fail','Pass','Pass','Pass','Fail'] df Output: First_name Last_name Marks Results 0 Ram Kumar 12 Fail 1 Mohan Sharma 52 Pass 2 Tina Ali 36 Pass 3 Jeetu Gandhi 85 Pass 4 Meera Kumari 23 Fail But when the column requires some computation or we need to add new values based on values in some column, then we can use for loop. Let's see how to create a column in pandas dataframe using for loop. In the given example a new column Result is created on the basis of marks in Marks column of the existing dataframe df. If the value in Marks column is greater than and equal to 33, then the value in the new column Result will be 'Pass' and if the value in Marks column is less than 0 and greater than 100 then value inserted in 'Result' column will be 'Invalid ' otherwise it should add value as 'Fail'. Example#2 Python3 # importing pandas import pandas as pd # Creating new dataframe initial_data = {'First_name': ['Ram', 'Mohan', 'Tina', 'Jeetu', 'Meera'], 'Last_name': ['Kumar', 'Sharma', 'Ali', 'Gandhi', 'Kumari'], 'Marks': [12, 52, 36, 85, 23] } df = pd.DataFrame(initial_data, columns = ['First_name', 'Last_name', 'Marks']) # Generate result using pandas result = [] for value in df["Marks"]: if value >= 33: result.append("Pass") elif value < 0 and value > 100: result.append("Invalid") else: result.append("Fail") df["Result"] = result print(df) Output: First_name Last_name Marks Result 0 Ram Kumar 12 Fail 1 Mohan Sharma 52 Pass 2 Tina Ali 36 Pass 3 Jeetu Gandhi 85 Pass 4 Meera Kumari 23 Fail Example#3 We can also use List comprehension to create a new column. Python3 df['Results'] = ['Pass' if m>=33 else 'Fail' for m in df['Marks']] df Output: First_name Last_name Marks Results 0 Ram Kumar 12 Fail 1 Mohan Sharma 52 Pass 2 Tina Ali 36 Pass 3 Jeetu Gandhi 85 Pass 4 Meera Kumari 23 Fail Comment More infoAdvertise with us Next Article Create a column using for loop in Pandas Dataframe J jitender_1998 Follow Improve Article Tags : Technical Scripter Python Technical Scripter 2018 Python-pandas Python pandas-dataFrame pandas-dataframe-program +2 More Practice Tags : python Similar Reads Create a pandas column using for loop Letâs see how to create a column in pandas dataframe using for loop. Such operation is needed sometimes when we need to process the data of dataframe created earlier for that purpose, we need this type of computation so we can process the existing data and make a separate column to store the data. I 2 min read Create a list from rows in Pandas DataFrame | Set 2 In an earlier post, we had discussed some approaches to extract the rows of the dataframe as a Python's list. In this post, we will see some more methods to achieve that goal. Note : For link to the CSV file used in the code, click here. Solution #1: In order to access the data of each row of the Pa 2 min read Pandas DataFrame assign() Method - Create new Columns in DataFrame The assign() method in Pandas is used to create or modify columns in a DataFrame while preserving the original DataFrame. It returns a new DataFrame with the specified modifications. This method allows adding single or multiple columns, performing calculations using existing columns and applying fun 4 min read Change Data Type for one or more columns in Pandas Dataframe When working with data in Pandas working with right data types for your columns is important for accurate analysis and efficient processing. Pandas offers several simple ways to change or convert the data types of columns in a DataFrame. In this article, we'll look at different methods to help you e 3 min read Create a Pandas DataFrame from Lists Converting lists to DataFrames is crucial in data analysis, Pandas enabling you to perform sophisticated data manipulations and analyses with ease. List to Dataframe Example# Simple listdata = [1, 2, 3, 4, 5]# Convert to DataFramedf = pd.DataFrame(data, columns=['Numbers'])Here we will discuss diffe 5 min read Like