diedro
diedro

Reputation: 623

add to a empty dataframe in pandas a row and assign an index

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.

Upvotes: 0

Views: 451

Answers (1)

Fayaz
Fayaz

Reputation: 28

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)

Upvotes: 1

Related Questions