我有很多用Matlab编程的经验,现在用Python编程,我就是搞不懂这个东西。。。我有一个数据帧,其中包含一列时间代码,如00:00:00.033。在
timecodes = ['00:00:01.001', '00:00:03.201', '00:00:09.231', '00:00:11.301', '00:00:20.601', '00:00:31.231', '00:00:90.441', '00:00:91.301']
df = pd.DataFrame(timecodes, columns=['TimeCodes'])
我所有的输入都是90秒或更少,所以我想创建一个只以秒为浮点的列。为此,我需要选择位置6结束并使其成为一个浮动,我可以对第一行进行如下操作:
^{pr2}$这很好用,但如果我现在想创建一个全新的列“Time_sec”,则以下操作不起作用:
df['Time_sec'] = float(df['TimeCodes'][:][6:])
因为df['TimeCodes'][:][6:]将第6行移到最后一行,而我希望在每一行中从第6行到最后一行。这也不起作用:
df['Time_sec'] = float(df['TimeCodes'][:,6:])
我需要绕一圈吗?一定有更好的办法。。。为什么df['TimeCodes'][:][6:]不起作用?在
您可以使用
slice
string方法,然后将整个内容转换为float:至于为什么
df['TimeCodes'][:][6:]
不起作用,它最终要做的是链接一些选择。首先获取与TimeCodes
列相关联的pd.Series
,然后从带有[:]
的序列中选择所有项,然后使用[6:]
选择索引为6或更高的项。在解决方案-indexing with str并由^{} 转换成{}:
相关问题 更多 >
编程相关推荐