Home > Blockchain >  Fill new column with yes if date has june else no
Fill new column with yes if date has june else no

Time:10-07

I have a situation in pandas dataframe where if date has June month then in new column print yes else no for other months

Date format 2019-06-01

CodePudding user response:

Since you haven't provided any details or sample dataframe, we can all only guess at what will actually work for your use-case. This solution assumes your "Date" column is the datetime type:

df["new"] = df.where(df["Date"].dt.month == 6, "yes", "no")

CodePudding user response:

def haveJune(row):
    if (row['Date'].str.contains('June').any()):
        val = 'Y'
    else:
        val = 'N'
    return val

df['new'] = df.apply(haveJune, axis=1)
  • Related