簡體   English   中英

使用python和pandas將時間戳列拆分為CSV中的兩個新列

[英]Split timestamp column into two new columns in CSV using python and pandas

我有一個超過210000行的大型CSV文件。 我是python和pandas的新手。 我想有效地遍歷timestamp列,將timestamp列拆分為2個新列(日期和時間),然后將新的日期列格式化為%Y%m%d並刪除新的time列。即,只寫回CSV文件新格式的日期列。 你怎么做到這一點 ?

輸入文件樣本:

   minit,timestamp,open,high,low,close
   0,2009-02-23 17:32:00,1.2708,1.2708,1.2706,1.2706
   1,2009-02-23 17:33:00,1.2708,1.2708,1.2705,1.2706
   2,2009-02-23 17:34:00,1.2706,1.2707,1.2702,1.2702
   3,2009-02-23 17:35:00,1.2704,1.2706,1.27,1.27
   4,2009-02-23 17:36:00,1.2701,1.2706,1.2698,1.2703
   5,2009-02-23 17:37:00,1.2703,1.2703,1.27,1.2702
   6,2009-02-23 17:38:00,1.2701,1.2701,1.2696,1.2697

輸出文件樣本:

   minit,date,open,high,low,close
   0,20090223,1.2708,1.2708,1.2706,1.2706
   1,20090223,1.2708,1.2708,1.2705,1.2706
   2,20090223,1.2706,1.2707,1.2702,1.2702
   3,20090223,1.2704,1.2706,1.27,1.27
   4,20090223,1.2701,1.2706,1.2698,1.2703
   5,20090223,1.2703,1.2703,1.27,1.2702
   6,20090223,1.2701,1.2701,1.2696,1.2697

我在Google搜索后開始編寫示例代碼來完成此操作:

     import csv
     import itertools
     import operator
     import time
     import datetime
     import pandas as pd
     from pandas import DataFrame, Timestamp
     from numpy import *

     def datestring_to_timestamp(str):
         return time.mktime(time.strptime(str, "%Y-%m-%d %H:%M:%S"))

     def timestamp_to_datestring(timestamp):
        return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(timestamp))

     def timestamp_to_float(str):
        return float(datetime.datetime.strptime(str, '%Y-%m-%d %H:%M:%S').strftime("%s"))

     def timestamp_to_intstring(str):
        return datetime.datetime.strptime(str, '%Y-%m-%d %H:%M:%S').strftime("%s")

    def timestamp_to_int(str):
        return int(datetime.datetime.strptime(str, '%Y-%m-%d %H:%M:%S').strftime("%s"))

    with open("inputfile.csv", 'rb') as input, open('outputfile.csv', 'wb') as output:
       reader = csv.reader(input, delimiter = ',')
       writer = csv.writer(output, delimiter = ',')

    # Need to process loop or process the timestamp column 

您可以在to_csv的參數中指定日期格式字符串,該字符串to_csv您的喜好輸出日期,而無需提取/轉換/添加新列等。

因此,使用read_csv加載數據:

df = pd.read_csv('mydata.csv', parse_dates=['timestamp']

In [15]:

df
Out[15]:
   minit           timestamp    open    high     low   close
0      0 2009-02-23 17:32:00  1.2708  1.2708  1.2706  1.2706
1      1 2009-02-23 17:33:00  1.2708  1.2708  1.2705  1.2706
2      2 2009-02-23 17:34:00  1.2706  1.2707  1.2702  1.2702
3      3 2009-02-23 17:35:00  1.2704  1.2706  1.2700  1.2700
4      4 2009-02-23 17:36:00  1.2701  1.2706  1.2698  1.2703
5      5 2009-02-23 17:37:00  1.2703  1.2703  1.2700  1.2702
6      6 2009-02-23 17:38:00  1.2701  1.2701  1.2696  1.2697

您可以根據需要在此階段重命名該列,然后可以將參數date_format='%Y%m%d' to to_csv`,這只會將日期部分輸出到csv,我們可以重新加載它並顯示什么它保存了:

In [19]:

df.rename(columns={'timestamp':'date'},inplace=True)
df.to_csv(r'c:\data\date.csv', date_format='%Y%m%d')
df1 = pd.read_csv(r'C:\data\date.csv', index_col=[0])
df1
Out[19]:
   minit      date    open    high     low   close
0      0  20090223  1.2708  1.2708  1.2706  1.2706
1      1  20090223  1.2708  1.2708  1.2705  1.2706
2      2  20090223  1.2706  1.2707  1.2702  1.2702
3      3  20090223  1.2704  1.2706  1.2700  1.2700
4      4  20090223  1.2701  1.2706  1.2698  1.2703
5      5  20090223  1.2703  1.2703  1.2700  1.2702
6      6  20090223  1.2701  1.2701  1.2696  1.2697

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM