create dataframe with date
import pandas as pd
import numpy as np
np.random.seed(0)
# create an array of 5 dates starting at '2015-02-24', one per minute
rng = pd.date_range('2015-02-24', periods=5, freq='T')
df = pd.DataFrame({ 'Date': rng, 'Val': np.random.randn(len(rng)) })
print (df)
# Output:
# Date Val
# 0 2015-02-24 00:00:00 1.764052
# 1 2015-02-24 00:01:00 0.400157
# 2 2015-02-24 00:02:00 0.978738
# 3 2015-02-24 00:03:00 2.240893
# 4 2015-02-24 00:04:00 1.867558
# create an array of 5 dates starting at '2015-02-24', one per day
rng = pd.date_range('2015-02-24', periods=5, freq='D')
df = pd.DataFrame({ 'Date': rng, 'Val' : np.random.randn(len(rng))})
print (df)
# Output:
# Date Val
# 0 2015-02-24 -0.977278
# 1 2015-02-25 0.950088
# 2 2015-02-26 -0.151357
# 3 2015-02-27 -0.103219
# 4 2015-02-28 0.410599
# create an array of 5 dates starting at '2015-02-24', one every 3 years
rng = pd.date_range('2015-02-24', periods=5, freq='3A')
df = pd.DataFrame({ 'Date': rng, 'Val' : np.random.randn(len(rng))})
print (df)
# Output:
# Date Val
# 0 2015-12-31 0.144044
# 1 2018-12-31 1.454274
# 2 2021-12-31 0.761038
# 3 2024-12-31 0.121675
# 4 2027-12-31 0.443863
Are there any code examples left?
New code examples in category Python
-
Python 2023-04-11 03:04:20
-
Python 2022-03-27 22:40:04 pycharm no module named
-
Python 2022-03-27 22:25:05 assign multiple variablesin one line
-
Python 2022-03-27 22:20:02 levenshtein distance
-
Python 2022-03-27 21:35:09 get text from url python last slash
-
Python 2022-03-27 21:30:30 df concatenate df
-
Python 2022-03-27 21:25:09 python odd or even
-
Python 2022-03-27 21:15:32 python include function from another file
-
Python 2022-03-27 21:10:01 color module python
-
Python 2022-03-27 21:00:27 python tkinter cursor types