2

I have a dataframe which gets da Date from a calendar and extracts some feature out of the date.

def processDate(self,date):
    WEEKDAY_MAP = {0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 7}
    df = pandas.DataFrame(data=[date], columns = ['DATE'])
    df['DATE'] = pandas.to_datetime(df['DATE'])
    df['DATE'] = df['DATE'].astype(str)
    df['MONTH'] = pandas.DatetimeIndex(df['DATE']).month
    df['WEEKDAY'] = pandas.DatetimeIndex(df['DATE']).dayofweek
    df['WEEKDAY'] = df['WEEKDAY'].map(WEEKDAY_MAP)
    df['HOLIDAY'] = '0'
    set_holiday(df)
    df['INTERVALL'] = '1'
    df.append([df]*5,ignore_index=True)
    print(df)

Console Log:

     DATE        MONTH  WEEKDAY HOLIDAY INTERVALL
     2017-09-13     9     3      0      1

What i need: duplicate the entry 48 times and increase the INTERVALL Value.

Outcome should be like this:

Console Log:

     DATE        MONTH  WEEKDAY HOLIDAY INTERVALL
     2017-09-13     9     3      0      1
     2017-09-13     9     3      0      2
     2017-09-13     9     3      0      3
     2017-09-13     9     3      0      4
     2017-09-13     9     3      0      5
     ...
     2017-09-13     9     3      0      48

I tried df.append([df]*48,ignore_index=True) but that didnt work.

3 Answers 3

3

Use np.repeat and create a new dataframe.

df = pd.DataFrame(df.values.repeat(48, axis=0), columns=df.columns)
df['INTERVALL'] = df.index + 1

df.head(10)

         DATE MONTH WEEKDAY HOLIDAY  INTERVALL
0  2017-09-13     9       3       0          1
1  2017-09-13     9       3       0          2
2  2017-09-13     9       3       0          3
3  2017-09-13     9       3       0          4
4  2017-09-13     9       3       0          5

df.shape
(48, 5)
Sign up to request clarification or add additional context in comments.

4 Comments

Don't think they want two interval columns but that's minor.
@cᴏʟᴅsᴘᴇᴇᴅ Or rather a non-misspelling :)
:) Just follow the out put you have , so I got a 'misspelling' too...:)
@Wen LOL... 1 sec... need to undo some votes to +1 you
3

Or using pd.concat

df = pd.concat([df]*48,axis=0).reset_index()
df['INTERVAL'] = df.index+ 1

Comments

1

You can use your own idea and then assign a range to the INTERVALL column

df= df.append([df]*47,ignore_index=True)
df["INTERVALL"] = range(1,49)

Note that you need to duplicate 47 times and then use the range from 1 till 48.

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.