Sign Up

Sign In

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

You must login to ask question.

Creating pandas dataframe with datetime index and random values in column

  • 3

How do I create a pandas dataframe with datetime as index, and random values for a column. Currently, I have this:

from datetime import datetime, timedelta

date_today = datetime.now()
date_end = date_today + timedelta(7)

df = pd.DataFrame(columns=['test'])

How do I proceed from here?

Share

1 Answer

  1. You can try this:

    import pandas as pd
    import numpy as np
    from datetime import datetime, timedelta
    
    date_today = datetime.now()
    days = pd.date_range(date_today, date_today + timedelta(7), freq='D')
    
    np.random.seed(seed=1111)
    data = np.random.randint(1, high=100, size=len(days))
    df = pd.DataFrame({'test': days, 'col2': data})
    df = df.set_index('test')
    print(df)
    
    
    test                            
    2017-03-22 10:07:41.914019    29
    2017-03-23 10:07:41.914019    56
    2017-03-24 10:07:41.914019    82
    2017-03-25 10:07:41.914019    13
    2017-03-26 10:07:41.914019    35
    2017-03-27 10:07:41.914019    53
    2017-03-28 10:07:41.914019    25
    2017-03-29 10:07:41.914019    23
    
    • 0
Leave an answer

Leave an answer

Browse

close