0

I would like to add row to a dataframe and assign a index. I try to make myself clear with the following example:

This is my empty dataframe:

dfr = pd.DataFrame(columns=['A','B'])

after that I would like to add some row with an index that depends on a specific day of the week.

Here the code:

for i in range(0,4):
    
     dfr = dfr.append({'A':  i,'B': i+10}, index=[day])
    
     dayw = day.strftime('%A')
     if dayw == 'Monday':
        delta = pd.Timedelta("3 days")
     if dayw == 'Wensday':    
        delta = pd.Timedelta("4 days")
        
     day = day + delta
        
    
     print(day)

As you can notice, I am not able to deal with the index. Thanks for any kind of help.

1
  • day is not defined, so I can't run your code. Commented Mar 17, 2022 at 16:56

1 Answer 1

1
dfr = pd.DataFrame(columns=['A','B'])
from datetime import datetime as dt
for i in range(0,4):
     day = dt.now()
     dfr1 = pd.DataFrame({'A':  i,'B': i+10}, index=[day])
     dayw = day.strftime('%A')
     delta = '0 days'
     if dayw == 'Monday':
        delta = pd.Timedelta("3 days")
     if dayw == 'Wensday':    
        delta = pd.Timedelta("4 days")
     if dayw == 'Thursday':    
        delta = pd.Timedelta("5 days")
     day = day + delta
     dfr = dfr.append(dfr1)
Sign up to request clarification or add additional context in comments.

2 Comments

it works. thanks. I have just put delta = pd.Timedelta("0 days") and not delta = '0 days'
Would it be possible to substitute append with concat?

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.