将多个列转换为epoch pandas

时间:2017-01-05 17:22:41

标签: pandas numpy machine-learning epoch

我正在尝试将以下列转换为epoch以准备机器学习我的其余csv包含字符串所以我假设这是最好的方法,我试图创建一个numpy数组并使用datetime转换它,等但这不起作用我有4列我试图从dd / mm / yyyy转换为纪元? 我试过这个方法

epoch_time = (1/2/2017 - datetime(1/1/1970)).total_seconds()

但我有4列,我想要转换所有这些,非常感谢

state                       object
car_model                   object
car_make                    object
car_year                   float64
drive_date                  object
id                           int64
register_date       datetime64[ns]
profile_date                object
add_profile_date            object
dtype: object

id: 23432   state:ohio  car_model:ford car_make:fusion car_year:2016 drive_date:1/1/2017 register_date:12/25/2016 profile_date:12/25/2016 add_profile_date: 12/25/2016

1 个答案:

答案 0 :(得分:1)

试试这个:

来源DF:

1: dGhpcyxpcyxhLGNzdixzdHJpbmcNCnRoaXMsaXMsYW5vdGhlcixzdWNoLHN0cmluZw== (dGhpcyxpcyxhLGNzdixzdHJpbmcNCnRoaXMsaXMsYW5vdGhlcixzdWNoLHN0cmluZw==\r\n)
        Line 1 with insufficient delimiters (,) encountered. Trying to see if it's encoded. length 68 line: dGhpcyxpcyxhLGNzdixzdHJpbmcNCnRoaXMsaXMsYWdWNoLHN0cmluZw==
2: dGhpcyxpcyxhLGNzdixzdHJpbmcNCnRoaXMsaXMsYW5vdGhlcixzdWNoLHN0cmluZw== (dGhpcyxpcyxhLGNzdixzdHJpbmcNCnRoaXMsaXMsYW5vdGhlcixzdWNoLHN0cmluZw==\r\n)
        Line 2 with insufficient delimiters (,) encountered. Trying to see if it's encoded. length 68 line: dGhpcyxpcyxhLGNzdixzdHJpbmcNCnRoaXMsaXMsYWdWNoLHN0cmluZw==
3: this,is,a,csv,string (this,is,a,csv,string\r\n)
        5 item line 3 OK: this,is,a,csv,string
4: this,is,another,such,string (this,is,another,such,string\r\n)
        5 item line 4 OK: this,is,another,such,string
5: this,is,a,csv,string (this,is,a,csv,string\r\n)
        5 item line 5 OK: this,is,a,csv,string
6: this,is,another,such,string (this,is,another,such,string\r\n)
        5 item line 6 OK: this,is,another,such,string

让我们选择In [173]: df Out[173]: id state car_model car_make car_year drive_date register_date profile_date add_profile_date 0 23432 ohio ford fusion 2016 1/1/2017 2016-12-25 12/25/2016 12/25/2016 In [174]: df.dtypes Out[174]: id int64 state object car_model object car_make object car_year int64 drive_date object register_date datetime64[ns] profile_date object add_profile_date object dtype: object 列:

date

首先转换"字符串"约会到Pandas datetime,然后将其转换为UNIX纪元

In [175]: date_cols = df.columns[df.columns.str.contains('_date')]

In [176]: date_cols
Out[176]: Index(['drive_date', 'register_date', 'profile_date', 'add_profile_date'], dtype='object')