Generation

generate functionMon, 06 Feb 2023

There is a df Pandas dataframe: date av_temp deviations country 1 1743-12-01 NaN NaN Åland 2 1744-01-01 NaN NaN Åland 3 1744-02-01 NaN NaN Åland 4 1744-03-01 NaN NaN Åland 9 1744-08-01 NaN NaN Åland Create a new column year from date in which you convert all date values to years. Example: 1744-01-01 Date column: 1744

def convert_to_year(x): try: date = pd.to_datetime(x) return date.year except: return None df['year'] = df.date.apply(convert_to_year)

Questions about programming?Chat with your personal AI assistant