- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我读取了一个包含三列的表:ID、时间和位置来创建数据帧。 ID 是第一个索引,时间是第二个索引。我希望时间频率为五分钟,如果对应时间没有数据,则将位置设置为最后一个位置(见下表)。
ID time place
001 00:00:00 1
00:15:00 3
002 00:05:00 2
ID time place
001 00:00:00 1
00:05:00 1
00:10:00 1
00:15:00 3
#continue to fill the table until 23:55:00
002 00:00:00 2
00:05:00 2
#continue to fill the table until 23:55:00
def create_table(n):
table = pd.read_table(n,sep='\x01', header=None, names=['ID','time','place'])
table['time'] = pd.to_datetime(table['time'], format='%H:%M:%S')
table = table.set_index('ID','time')
return table
我不知道如何继续制作理想的数据框。有人能给我一些提示吗?非常感谢!!
最佳答案
要获取上面定义的 DataFrame,我们可以这样做:
df = pd.DataFrame({'ID': ['001', '001', '002'], 'time': ['00:00:00', '00:15:00', '00:05:00'], 'place': [1, 3, 2]}).set_index(['ID', 'time'])
为了能够只处理时间并对每个 ID 值应用相同的操作,让我们取消堆叠“ID”,以便我们拥有一个列的多重索引,其中 ID 位于顶层:
In [91]: df = df.unstack(0)
In [92]: df
Out[92]:
place
ID 001 002
time
00:00:00 1.0 NaN
00:05:00 NaN 2.0
00:15:00 3.0 NaN
现在,让我们将索引(现在只是“时间”)转换为 DatetimeIndex:
In [93]: df.index = pd.to_datetime(df.index)
In [94]: df
Out[94]:
place
ID 001 002
time
2017-06-06 00:00:00 1.0 NaN
2017-06-06 00:05:00 NaN 2.0
2017-06-06 00:15:00 3.0 NaN
这会添加今天的日期,但我们可以稍后删除它。
接下来,让我们创建另一个 DatetimeIndex,其中包含今天日期的 5 分钟增量:
In [95]: times = pd.date_range("00:00:00", "23:55:00", freq="5min")
In [96]: times
Out[96]:
DatetimeIndex(['2017-06-06 00:00:00', '2017-06-06 00:05:00',
'2017-06-06 00:10:00', '2017-06-06 00:15:00',
'2017-06-06 00:20:00', '2017-06-06 00:25:00',
'2017-06-06 00:30:00', '2017-06-06 00:35:00',
'2017-06-06 00:40:00', '2017-06-06 00:45:00',
...
'2017-06-06 23:10:00', '2017-06-06 23:15:00',
'2017-06-06 23:20:00', '2017-06-06 23:25:00',
'2017-06-06 23:30:00', '2017-06-06 23:35:00',
'2017-06-06 23:40:00', '2017-06-06 23:45:00',
'2017-06-06 23:50:00', '2017-06-06 23:55:00'],
dtype='datetime64[ns]', length=288, freq='5T')
让我们根据这个新的 DatetimeIndex 重新索引 df 的索引:
In [97]: df = df.reindex(times)
In [98]: df
Out[98]:
place
ID 001 002
2017-06-06 00:00:00 1.0 NaN
2017-06-06 00:05:00 NaN 2.0
2017-06-06 00:10:00 NaN NaN
2017-06-06 00:15:00 3.0 NaN
2017-06-06 00:20:00 NaN NaN
...
现在我们只需要前向填充,以便每次该位置都是最后一个非 NaN 位置:
In [99]: df = df.ffill()
In [100]: df
Out[100]:
place
ID 001 002
2017-06-06 00:00:00 1.0 NaN
2017-06-06 00:05:00 1.0 2.0
2017-06-06 00:10:00 1.0 2.0
2017-06-06 00:15:00 3.0 2.0
2017-06-06 00:20:00 3.0 2.0
2017-06-06 00:25:00 3.0 2.0
2017-06-06 00:30:00 3.0 2.0
...
从这里开始,我们需要去掉日期:
In [101]: df.index = df.index.strftime('%H:%M:%S')
In [102]: df
Out[102]:
place
ID 001 002
00:00:00 1.0 NaN
00:05:00 1.0 2.0
00:10:00 1.0 2.0
00:15:00 3.0 2.0
00:20:00 3.0 2.0
00:25:00 3.0 2.0
...
我们在“时间”索引上丢失了名称,所以让我们把它放回去:
df.index = df.index.set_names('time')
最后,将“ID”放回到索引中:
In [103]: df.stack(1).swaplevel(0, 1)
Out[103]:
place
ID time
001 00:00:00 1.0
00:05:00 1.0
002 00:05:00 2.0
001 00:10:00 1.0
002 00:10:00 2.0
001 00:15:00 3.0
...
关于python - 填充DataFrame,缺失数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44396120/
我编写了一个 Android 应用程序,它使用 Azure 来执行用户通过 Google、Twitter 和 Facebook 的登录;它使用 Microsoft.WindowsAzure.Mobil
我想将 AdomdClient 引用添加到 C# 项目,但它不在引用列表中。客户端列在程序集文件夹 C:\Windows\Assembly 中。 计算机上安装了 SQL Server 2012。 最佳
我正在学习“绘图应用程序”的教程。当我在 Firefox 上启动我的应用程序时,Firebug 告诉我“在语句之前缺少 ;” 我在第 9 行调用函数的位置。我只是不明白应该将这些“;”放在哪里. va
我想将 AdomdClient 引用添加到 C# 项目,但它不在引用列表中。客户端列在程序集文件夹 C:\Windows\Assembly 中。 计算机上安装了 SQL Server 2012。 最佳
我在 Firebug 中不断收到关于 onClick 事件的错误。 我已经尝试了 "和 ' 的各种不同组合,但无济于事。在添加 onClick 事件之前,这工作正常。 有人能发现我可能做错了什么吗?
Visual Studio 2015 告诉我找不到 WSASetSocketSecurity。 该 dll 存在并且还包括似乎没问题。 我的包括: windows.h stdio.h Wincrypt
我需要访问 eloquent 的 whereHasNot方法(此处添加: https://github.com/laravel/framework/commit/8f0cb08d8ebd157cbfe
跟随宠物物体检测的 TF 教程:https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/run
构建路径 > 添加库 > JUnit 无法添加 JUnit3 或 JUnit4 组件。 我在.log 中看到这样的消息 !MESSAGE No property tester contributes
我正在运行此脚本来查看网络上的摄像机: gst-launch udpsrc port=1234 ! "application/x-rtp, payload=127" ! rtph264depay !
我正在使用http://java.sun.com/jsp/jstl/fmt用于从 Spring 配置中设置的 Message Resource Bundle 输出消息的标签库。消息解析也可以放在 Co
我正在将 Ninject 与 MVC4 连接起来,并让它工作到尝试实际解决依赖关系的程度。但是,我收到以下异常: Method not found: 'System.Web.Http.Services
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 关闭 9 年前。 Improve
我在启动 ASP.NET MVC5 应用程序时遇到问题。到目前为止一切正常。启动应用程序时出现以下错误: Could not load file or assembly 'Microsoft.Appl
我已经使用以下方法创建了一个环境: conda create --prefix C:\Users\Dell\Dropbox\DjangoProjects\webenv python=3.6 执行后:c
我们有一个遗留的 Web 窗体应用程序,我们最近将其从网站项目转换为 Web 应用程序项目。 Web 窗体项目是解决方案的“启动”项目。 有一个 MVC 项目是对 Web 窗体项目的引用。 在 MVC
使用某种字体,我使用Java的FontLayout来确定它的上升、下降和行距。 (参见 Java 的 FontLayout 教程 here) 在我的具体案例中,我使用的是 Arial Unicode
我正在尝试在 linux 下编译 qt ffmpeg 包装器简单编码/解码示例 QTFFmpegWrapper source # Set list of required FFmpeg librari
我正在使用来自开发人员 android 页面的 SlidingTabLayout.java。在我使用 slidingTabLayout.setDistributeEvenly(true); 使 sli
我正在尝试使用 v360 filter 将 180° 鱼眼视频转换为普通/常规视频的 FFmpeg . 这是我尝试过的命令:ffmpeg -i in.mp4 -vf "v360=input=fishe
我是一名优秀的程序员,十分优秀!