pandas 如何在时间序列数据上“get_dummies”

2023-12-02

如果我有一些时间序列数据:(弥补一些)

import numpy as np
import pandas as pd
np.random.seed(11)

rows,cols = 50000,2
data = np.random.rand(rows,cols) 
tidx = pd.date_range('2019-01-01', periods=rows, freq='H') 
df = pd.DataFrame(data, columns=['Temperature','Value'], index=tidx)

我该如何利用get_dummies?只是看着熊猫文档,我不知道我是否可以应用到我如何制作一种热编码。

例如,我知道如何制作虚拟变量来表示一周时间变量的唯一方法是一种非常笨重的冗余代码方法。有人可以给我建议如何做得更好吗?

#create dummy variables
df['month'] = df.index.month
df['year'] = df.index.year
df['day_of_week'] = df.index.dayofweek
df['hour'] = df.index.strftime('%H').astype('int')

df['hour_0'] = np.where(df['hour'].isin([0]), 1, 0)
df['hour_1'] = np.where(df['hour'].isin([1]), 1, 0)
df['hour_2'] = np.where(df['hour'].isin([2]), 1, 0)
df['hour_3'] = np.where(df['hour'].isin([3]), 1, 0)
df['hour_4'] = np.where(df['hour'].isin([4]), 1, 0)
df['hour_5'] = np.where(df['hour'].isin([5]), 1, 0)
df['hour_6'] = np.where(df['hour'].isin([6]), 1, 0)
df['hour_7'] = np.where(df['hour'].isin([7]), 1, 0)
df['hour_8'] = np.where(df['hour'].isin([8]), 1, 0)
df['hour_9'] = np.where(df['hour'].isin([9]), 1, 0)
df['hour_10'] = np.where(df['hour'].isin([10]), 1, 0)
df['hour_11'] = np.where(df['hour'].isin([11]), 1, 0)
df['hour_12'] = np.where(df['hour'].isin([12]), 1, 0)
df['hour_13'] = np.where(df['hour'].isin([13]), 1, 0)
df['hour_14'] = np.where(df['hour'].isin([14]), 1, 0)
df['hour_15'] = np.where(df['hour'].isin([15]), 1, 0)
df['hour_16'] = np.where(df['hour'].isin([16]), 1, 0)
df['hour_17'] = np.where(df['hour'].isin([17]), 1, 0)
df['hour_18'] = np.where(df['hour'].isin([18]), 1, 0)
df['hour_19'] = np.where(df['hour'].isin([19]), 1, 0)
df['hour_20'] = np.where(df['hour'].isin([20]), 1, 0)
df['hour_21'] = np.where(df['hour'].isin([21]), 1, 0)
df['hour_22'] = np.where(df['hour'].isin([22]), 1, 0)
df['hour_23'] = np.where(df['hour'].isin([23]), 1, 0)

df['monday'] = np.where(df['day_of_week'].isin([0]), 1, 0)
df['tuesday'] = np.where(df['day_of_week'].isin([1]), 1, 0)
df['wednesday'] = np.where(df['day_of_week'].isin([2]), 1, 0)
df['thursday'] = np.where(df['day_of_week'].isin([3]), 1, 0)
df['friday'] = np.where(df['day_of_week'].isin([4]), 1, 0)
df['saturday'] = np.where(df['day_of_week'].isin([5]), 1, 0)
df['sunday'] = np.where(df['day_of_week'].isin([6]), 1, 0)

df['january'] = np.where(df['month'].isin([1]), 1, 0)
df['february'] = np.where(df['month'].isin([2]), 1, 0)
df['march'] = np.where(df['month'].isin([3]), 1, 0)
df['april'] = np.where(df['month'].isin([4]), 1, 0)
df['may'] = np.where(df['month'].isin([5]), 1, 0)
df['june'] = np.where(df['month'].isin([6]), 1, 0)
df['july'] = np.where(df['month'].isin([7]), 1, 0)
df['august'] = np.where(df['month'].isin([8]), 1, 0)
df['september'] = np.where(df['month'].isin([9]), 1, 0)
df['october'] = np.where(df['month'].isin([10]), 1, 0)
df['november'] = np.where(df['month'].isin([11]), 1, 0)
df['december'] = np.where(df['month'].isin([12]), 1, 0)

df['year19'] = np.where(df['year'].isin([2019]), 1, 0)
df['year20'] = np.where(df['year'].isin([2020]), 1, 0)
df['year21'] = np.where(df['year'].isin([2021]), 1, 0)
df['year22'] = np.where(df['year'].isin([2022]), 1, 0)
df['year23'] = np.where(df['year'].isin([2023]), 1, 0)
df['year24'] = np.where(df['year'].isin([2024]), 1, 0)

然后我正在尝试 ML 算法的最终数据框将是:

df2 = df[['Temperature', 'Value', 
            'hour_0' , 'hour_1' , 'hour_2' , 'hour_3' , 'hour_4' , 'hour_5' , 'hour_6' ,
            'hour_7' , 'hour_8' , 'hour_9' , 'hour_10' , 'hour_11' , 'hour_12' , 'hour_13' , 
            'hour_14' , 'hour_15' , 'hour_16' , 'hour_17' , 'hour_18' , 'hour_19' , 'hour_20' , 
            'hour_21' , 'hour_22' , 'hour_23' , 
            'monday' , 'tuesday' , 'wednesday' , 'thursday' , 'friday' , 'saturday' , 'sunday' , 
            'january' , 'february' , 'march' , 'april' , 'may' , 'june' , 'july' , 'august' , 
            'september' , 'october' , 'november' , 'december' , 
            'year19' , 'year20' , 'year21' , 'year22' , 'year23' , 'year24']]

EDIT更新的代码尝试

import numpy as np
import pandas as pd
np.random.seed(11)

rows,cols = 50000,2
data = np.random.rand(rows,cols) 
tidx = pd.date_range('2019-01-01', periods=rows, freq='H') 
df = pd.DataFrame(data, columns=['Temperature','Value'], index=tidx)

df['hour'] = df.index.strftime('%H').astype('int')
df['day_of_week'] = df.index.dayofweek
df['month'] = df.index.month
df['year'] = df.index.year

hour_dummies = pd.get_dummies(df['hour'], prefix='hour')

day_mapping = {0: 'monday', 1: 'tuesday', 2: 'wednesday', 3: 'thursday', 4: 'friday', 5: 'saturday', 6: 'sunday'}
day_dummies = pd.get_dummies(df['day_of_week'].map(day_mapping))

month_mapping = {0: 'jan', 1: 'feb', 2: 'mar', 3: 'apr', 4: 'may', 5: 'jun', 6: 'jul',
                 7: 'aug', 8: 'sep', 9: 'oct', 10: 'nov', 11: 'dec'}
month_dummies = pd.get_dummies(df['month'].map(month_mapping))

year_mapping = {0: 'year_2019', 1: 'year_2020', 2: 'year_2021', 3: 'year_2022', 4: 'year_2023', 5: 'year_2024'}
year_dummies = pd.get_dummies(df['year'].map(year_mapping))

df = df.join(hour_dummies)
df = df.join(day_dummies)
df = df.join(month_dummies)
df = df.join(year_dummies)

可以从时间索引中提取对应的信息,然后使用pd.get_dummies。例如

# day name
day_names = pd.get_dummies(df.index.day_name())

# hours
hours = pd.get_dummies(df.index.hour, prefix='hour')

# months
months = pd.get_dummies(df.index.month_name())

# year
years = pd.get_dummies(df.index.year, prefix='year')

进而concat:

df = pd.concat((df, hours, day_names), axis=1)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

pandas 如何在时间序列数据上“get_dummies” 的相关文章

随机推荐

  • 从 SwiftUI 上的部分列表中删除项目

    Overview 我正在使用核心数据做一个简单的应用程序我有两个实体用户和地区该应用程序按地区显示用户列表问题是在删除操作中 如果我尝试删除第二个部分 则列表会从第一个部分中删除用户用户从第二部分删除第二个用户从第一部分 我认为索引集发送该
  • Resteasy安全拦截器-如何在拦截器内获取客户端IP地址?

    我已经实现了一个拦截器 通过以下注释对客户端 IP 地址进行安全检查 提供者 服务器拦截器 优先级 安全 preprocess 方法采用参数 HttpRequest 请求 ResourceMethod 方法 有没有办法从 Resteasy
  • PHP 文件上传 - CGI 上的内存处理

    我想知道 PHP 如何处理文件上传 我是read文件上传将被分割成块 并在上传完成后组装 这个事实有证实吗 这将有助于了解文件上传的处理和设置 mod php 或 CGI 的处理相同吗 mod php 与 Apache 的联系更加紧密 CG
  • Rails 在患有 STI 时设计登记表

    我不知道如何创建工人和协会 所以我能够将它们联系在一起 我在用户中有一个类型列 这是我的表单 http localhost 3000 workers sign up h2 Create Worker h2 p br p p b b p
  • 我是否需要付费计划才能在 firebase 功能上使用 recaptcha?

    我创建了一个 firebase 函数来进行网站的 recapcha 验证 但我收到了人们在拥有 Spark 免费 计划并尝试向外部 api 发送请求时遇到的错误 这是我的错误 Firebase 对所有 http 请求执行 ENOTFOUND
  • python的“set”稳定吗?

    这个问题是在回答另一个SO问题时出现的 there 当我对 python 集进行多次迭代 在调用之间不更改它 时 我可以假设它总是以相同的顺序返回元素吗 如果不是 改变顺序的理由是什么 是确定性的还是随机的 或者定义的实现 当我重复调用同一
  • 如何使用 Sass 获取动态列表中的项目数?

    ul li li li li li li li li ul 是否可以使用 Sass 获取动态列表的最后一项的 n 值 就像使用 each 或 for 语句计算 ul 中的项目数一样 萨斯号only编译为 CSS 它永远不会发送到浏览器 它不
  • hdf5 Java 库入门

    我正在用 jhdf5 学习 HDF5 我正在 MAC OS X 上工作 酿造安装hdf5 这会将 hdf5 1 10 安装在 usr local Cellar hdf5 中 复制此文件并将其放入 gradle 项目中 https suppo
  • SCons 在 Windows 中启动超慢

    长期以来 我在使用 SCons 进行构建时一直饱受启动时间过长的困扰 在我的旧工作笔记本电脑上 构建最基本的 hello world 示例可能需要长达 60 秒的时间 我刚刚收到一台新笔记本电脑 所以我有机会进一步调查这一点 我们的笔记本电
  • 将 LINQ 表达式谓词从一种类型更改为另一种类型

    我有两个不相关的课程 一种作为 API 公开 另一种由 3rd 方 API 在内部使用 Entity 从我们的 API 公开 而 EntityProvider 来自第 3 方程序集 class Entity public A get set
  • stringstream 到底做了什么?

    我从昨天开始尝试学习 C 我正在使用这个文档 http www cplusplus com files tutorial pdf 第 32 页 我在文档中找到了一段代码并运行了它 我尝试输入价格为 5 5 卢比 数量为整数 但输出为 0 我
  • Android - 如何使标题(TextView)在工具栏中居中?

    在我的工具栏左侧有一个徽标 我想将 TextView 放在同一个工具栏中作为我的标题 由于某种原因 标题没有居中 而是位于右侧 这是我的尝试
  • 使用 Perl / AWK 将两连续行合并为一行

    我有如下数据 abcd join abcd efgh join efgh 我想将两个连续的对连接成一行 结果 abcd join abcd efgh join efgh 我怎样才能在 Perl AWK 中做到这一点 sed N s n in
  • Crystal Report 可重新分发用于 Windows Server 2008? - 尝试加载 Crystal Reports 运行时时发生错误

    首先 谢谢大家 我需要什么才能在 Windows Server 2008 上获取工作 Crystal Report 或者我如何在 Windows Server 2008 上运行的网站 框架 3 5 4 0 上获取工作 rpt 文件 由代码加
  • .htaccess 将子域重定向到域

    我有一个子域http sub domain com 我希望来自该子域的所有链接都重定向到目标域 与 www 非 www 以及重定向路径 重定向 www http www sub domain com 非万维网 http sub domain
  • 电子邮件中支持 JavaScript 吗?

    电子邮件中支持 JavaScript 吗 http en wikipedia org wiki Comparison of e mail clients 老客户 例如 Lotus Notes Mozilla Thunderbird Outl
  • Visual Studio 2008 中的“文件另存为”对话框问题

    我正在通过 Visual Studio 2008 进行编程 我更改了 html 页面并使用 Ctrl S 保存它 然后在浏览器上按 Ctrl F5 进行测试 我的问题是 当我在刷新浏览器后将文件保存在 VS 中时 会出现 文件另存为 对话框
  • 使用php在浏览器中显示.msg文件

    我已在 php 应用程序中附加了 Outlook 消息文件 我将该文件存储在 SQL Server 数据库中 现在我想从浏览器打开并显示它 我尝试了这段代码 if ext msg header ContentType application
  • 如何使用 jQuery 获取整个页面的 HTML?

    I used document html 但这引发了错误 有没有办法获得一切 你可以尝试 html html 如果您还想捕获 html 标签 您可以将它们连接到 html 如下所示 function getPageHTML return h
  • pandas 如何在时间序列数据上“get_dummies”

    如果我有一些时间序列数据 弥补一些 import numpy as np import pandas as pd np random seed 11 rows cols 50000 2 data np random rand rows co