Pandas Python - 将 HH:MM:SS 转换为聚合中的秒数(csv 文件) [英] Pandas Python - convert HH:MM:SS into seconds in aggegate (csv file)
问题描述
我正在尝试将 'Avg.在 Pandas read_csv
模块/函数中将会话持续时间'(HH:MM:SS) 列转换为整数(以秒为单位).例如,0:03:26"将是转换后的 206 秒.
I'm trying to convert the numbers in 'Avg. Session Duration'(HH:MM:SS) column into whole numbers (in seconds) in Pandas read_csv
module/function.
For instance, '0:03:26' would be 206 seconds after the conversion.
输入示例:
Source Month Sessions Bounce Rate Avg. Session Duration
ABC.com 201501 408 26.47% 0:03:26
EFG.com 201412 398 31.45% 0:04:03
我写了一个函数:
def time_convert(x):
times = x.split(':')
return (60*int(times[0])+60*int(times[1]))+int(times[2])
这个函数工作正常,只需将 '0:03:26' 传递给函数.但是,当我尝试通过将函数应用于 Pandas 中的另一列来创建新列持续时间"时,
This function works just fine while simply passing '0:03:26' to the function. But when I was trying to create a new column 'Duration' by applying the function to another column in Pandas,
df = pd.read_csv('myfile.csv')
df['Duration'] = df['Avg. Session Duration'].apply(time_convert)
它返回了一个错误信息:
It returned an Error Message:
> --------------------------------------------------------------------------- AttributeError Traceback (most recent call
> last) <ipython-input-53-01e79de1cb39> in <module>()
> ----> 1 df['Avg. Session Duration'] = df['Avg. Session Duration'].apply(lambda x: x.split(':'))
>
> /Users/yumiyang/anaconda/lib/python2.7/site-packages/pandas/core/series.pyc
> in apply(self, func, convert_dtype, args, **kwds) 1991
> values = lib.map_infer(values, lib.Timestamp) 1992
> -> 1993 mapped = lib.map_infer(values, f, convert=convert_dtype) 1994 if len(mapped) and
> isinstance(mapped[0], Series): 1995 from
> pandas.core.frame import DataFrame
>
> /Users/yumiyang/anaconda/lib/python2.7/site-packages/pandas/lib.so in
> pandas.lib.map_infer (pandas/lib.c:52281)()
>
> <ipython-input-53-01e79de1cb39> in <lambda>(x)
> ----> 1 df['Avg. Session Duration'] = df['Avg. Session Duration'].apply(lambda x: x.split(':'))
>
> AttributeError: 'float' object has no attribute 'split'
我不知道为什么它说平均"的值.会话持续时间'是浮动的.
I don't know why it says values of 'Avg. Session Duration' are float.
Data columns (total 7 columns):
Source 250 non-null object
Time 251 non-null object
Sessions 188 non-null object
Users 188 non-null object
Bounce Rate 188 non-null object
Avg. Session Duration 188 non-null object
% New Sessions 188 non-null object
dtypes: object(7)
谁能帮我找出问题所在?
Can someone help me figure out where the problem is?
推荐答案
该错误表示该列被识别为浮点数,而不是字符串.修复您读取数据的方式,例如:
The error means that the column is recognized as float, not string. Fix the way you read the data e.g.:
#!/usr/bin/env python
import sys
import pandas
def hh_mm_ss2seconds(hh_mm_ss):
return reduce(lambda acc, x: acc*60 + x, map(int, hh_mm_ss.split(':')))
df = pandas.read_csv('input.csv', sep=r'\s{2,}',
converters={'Avg. Session Duration': hh_mm_ss2seconds})
print(df)
输出
Source Month Sessions Bounce Rate Avg. Session Duration
0 ABC.com 201501 408 26.47% 206
1 EFG.com 201412 398 31.45% 243
[2 rows x 5 columns]
这篇关于Pandas Python - 将 HH:MM:SS 转换为聚合中的秒数(csv 文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!