Home > Net >  Iterating Date in python Dataframe
Iterating Date in python Dataframe

Time:05-16

I realize this is probably a very trivial question but I have a dataframe of 1000 rows and I want to create a new column "Date" but for a single date "2018-01-31". I tried the code below but python just returns "Length of values (1) does not match length of index"

I would really appreciate any help!

Date = ['2018-01-31']
for i in range(len(Output)): 
    Output['Date']= Date

CodePudding user response:

Assuming Output is the name of your pandas dataframe with 1000 rows you can do:

Output['Date'] = "2018-01-31"

or using the datetime library you could do:

from datetime import date

Output["Date"] = date(2018, 1, 31)

to format it as a date object rather than a string. You also do not need to iterate over each row if you are wanting the same value for each row. Simply adding a new column with the value will set the value of the new column to the assigned value for each row.

  • Related