Sunday, November 17, 2024
Google search engine
HomeLanguagesCreate a column using for loop in Pandas Dataframe

Create a column using for loop in Pandas Dataframe

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

RELATED ARTICLES

Most Popular

Recent Comments