Pandas Python - konvertieren HH:MM:SS in Sekunden in aggegate (csv-Datei)

Ich versuche zum konvertieren der zahlen in der " Avg. Die Dauer der Sitzung'(HH:MM:SS) Spalte in ganzen zahlen (in Sekunden), in Pandas read_csv Modul/Funktion.
Zum Beispiel, '0:03:26" wäre 206 Sekunden nach der Konvertierung.

Eingang Beispiel:

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

Schrieb ich eine Funktion:

def time_convert(x):
    times = x.split(':')
    return (60*int(times[0])+60*int(times[1]))+int(times[2])

Diese Funktion arbeitet nur in Ordnung, wenn Sie einfach vorbei '0:03:26', die an die Funktion. Aber wenn ich versuche, erstellen Sie eine neue Spalte 'Dauer' durch die Anwendung der Funktion auf eine andere Spalte in Pandas,

df = pd.read_csv('myfile.csv')
df['Duration'] = df['Avg. Session Duration'].apply(time_convert)

Wieder eine Fehlermeldung:

> --------------------------------------------------------------------------- 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'

Ich weiß nicht, warum es sagt, dass die Werte der " Avg. Die Dauer der Sitzung " sind Schwimmer.

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)

Kann jemand mir helfen herauszufinden, wo das problem ist?

InformationsquelleAutor Yumi | 2015-03-04
Schreibe einen Kommentar