Home > other >  Add new row to a DataFrame based on condition
Add new row to a DataFrame based on condition

Time:10-27

I have the following DataFrame (I'm sorry for posting an image but I dont know how to write tables with more than 2 columns here on Stackoverflow lol)

enter image description here

It is missing the fifth month as you can see.

How can I detect and add a new row that contains the fifth month to get a new DataFrame with all months?

enter image description here

import pandas as pd

data = {'YEAR1': [2020]*11, 'YEAR2': [2019]*11, 'MONTH': [1,2,3,4,6,7,8,9,10,11,12]}  

df = pd.DataFrame(data)  

CodePudding user response:

df.loc[len(df.index)] = [2020, 2019, 5]
df.sort_values(by="MONTH", inplace=True)

CodePudding user response:

I'm guessing this is a model question meant to get an answer for a harder one. This wouldn't be the most wieldly answer in all use-cases, but for what you described, a simple

if 5 not in df['MONTH'].values:
    df.loc[len(df.index)] = [2020, 2021, 5]

would suffice.

  • Related