0

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

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.