【问题标题】:How can I create a dataframe?如何创建数据框?
【发布时间】:2015-06-10 17:12:49
【问题描述】:

输出[21]:

日期

2012-04-12 14:56:50    1.25640
2012-04-12 15:11:55    1.43075
2012-04-12 15:27:01    1.36991
2012-04-12 15:42:06    1.35935
2012-04-12 15:57:10    1.30568
2012-04-12 16:12:10    1.28775
2012-04-12 16:27:14    1.24597
2012-04-12 16:42:19    1.28228
2012-04-12 16:57:24    1.36571
2012-04-12 17:12:28    1.32013
2012-04-12 17:27:33    1.35489
2012-04-12 17:42:37    1.34368
2012-04-12 17:57:41    1.31422
2012-04-12 18:12:44    1.31197
2012-04-12 18:27:46    1.33898
...
2014-04-15 14:14:59    5.40786
2014-04-15 14:29:59    5.43847
2014-04-15 14:44:59    5.48222
2014-04-15 14:59:59    5.49327
2014-04-15 15:14:59    5.42679
2014-04-15 15:29:59    5.43036
2014-04-15 15:44:59    5.41471
2014-04-15 15:59:59    5.47004
2014-04-15 16:14:59    5.47507
2014-04-15 16:29:59    5.55595
2014-04-15 16:44:59    5.46151
2014-04-15 16:59:59    5.52125
2014-04-15 17:14:59    5.44116
2014-04-15 17:29:59    5.35836
2014-04-15 17:44:59    5.29439

名称:kwh,长度:65701

我有这个数据框,我想再创建 3 个包含年、月和时间的数据框。我该如何创建?

【问题讨论】:

  • 3 个数据帧?还是现有数据框中的 3 个新列会有所帮助?
  • 我想在 3 个不同的列中分离年、月和时间。我不知道方法..为了解释你我想在分离后做线性回归。如果你能帮助我.. ...

标签: python pandas dataframe


【解决方案1】:

让我们创建一个虚拟数据

start = datetime.datetime(2000, 1, 1)
end = datetime.datetime(2000, 1, 3)
d = pd.date_range(start, end, freq='H')
t_df = pd.DataFrame({'col1': np.random.random_integers(0, 10, d.size)}, index=d)

t_df

                   col1
2000-01-01 00:00:00 4
2000-01-01 01:00:00 8
2000-01-01 02:00:00 2
2000-01-01 03:00:00 3
2000-01-01 04:00:00 0
2000-01-01 05:00:00 1
2000-01-01 06:00:00 0
2000-01-01 07:00:00 10

现在,提取日期属性

t_df['year'] = t_df.index.year
t_df['month'] = t_df.index.month
t_df['day'] = t_df.index.day
t_df

                  col1  year  month day
2000-01-01 00:00:00 4   2000    1   1
2000-01-01 01:00:00 8   2000    1   1
2000-01-01 02:00:00 2   2000    1   1
2000-01-01 03:00:00 3   2000    1   1
2000-01-01 04:00:00 0   2000    1   1
2000-01-01 05:00:00 1   2000    1   1
2000-01-01 06:00:00 0   2000    1   1
2000-01-01 07:00:00 10  2000    1   1

阅读更多关于DatetimeIndex

【讨论】:

  • y = map(lambda x:x.year,kk.index)
  • 但是我不知道如何使用这个时间序列数据集设置 X 和 Y 进行线性回归。有什么想法吗?
  • 那将是一个不同的问题。 =)
  • 你有解决办法吗??
猜你喜欢
  • 2019-05-27
  • 2020-02-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多