Python更改csv文件中特定列的字符串(日期)格式

时间:2016-10-27 09:18:34

标签: python date csv pandas format

我尝试将csv文件中名为date的列的值转换为其他格式,例如:

原始记录

transfer id,player id,player name,season,date
732058,1126,,12/13,Jul 1- 2012
581951,1126,,11/12,Jun 3- 2011
295000,1126,,09/10,Aug 12- 2009
98459,1126,,06/07,Nov 6- 2006
7267,1126,,03/04,Jul 2- 2003
...

我希望得到像

这样的结果
transfer id,player id,player name,season,date
732058,1126,,12/13,2012-07-01
581951,1126,,11/12,2011-06-03
295000,1126,,09/10,2009-08-12
98459,1126,,06/07,2006-11-06
7267,1126,,03/04,2003-07-02
...

由于存储在csv文件中的数据是字符串,我编写了一个可以转换日期格式的方法:

import time

# convert date from original format to new format
def date_convert(_date,fmt_original,fmt_new):
    if date_validate(_date,fmt_original):
        timeArray=time.strptime(_date,fmt_original)
        return time.strftime(fmt_new,timeArray)
    else:
        return '0001-01-01'

def date_validate(_date,fmt_original):
    try:
        time.strptime(_date, fmt_original)
        return True
    except ValueError:
        return False

然后我尝试更改csv文件中的日期,并尝试使用pandas@MaxU告知:

我写了像

这样的代码
import pandas as pd
import date_format

df=pd.read_csv('the_transfer_info_test.csv',delimiter=',')
df.date=date_format.date_convert(df.date, '%b %d- %Y', '%Y-%m-%d')
print df

起初我得到了这样的例外:

TypeError: expected string or buffer

我认为它可能与数据类型有关,因为df.date在pandas中获得了一个Series类型,所以我编码为

df.date=date_format.date_convert(str(df.date), '%b %d- %Y', '%Y-%m-%d')

但它返回所有0001-01-01这是date_format中的例外日期,因此我搜索了如何将Series转换为String并找到类似的答案 @Amit,我尝试了以下方法:

df['date'].astype(basestring)
df.date.apply(str)
df['date'].astype(str)
df['date'].astype('str')

但他们不适合我,我得到了同样的例外:

TypeError: expected string or buffer

我想知道如何在csv文件中转换特定的列值,或者是否使用pandas。

BTW,我的python版本为2.7.12,IDE PyCharm和Anoconda 4.0.0以及pandas 0.18.0。

感谢任何帮助,谢谢。

感谢@jezrael,对于我上面的示例,它都运行良好,我的错是我的意思是简化我的问题并简化我的问题,实际上我的原始数据就像:

transfer id,player id,player name,season,date,move from,move from id,move to,move to id,market value,transfer fee
732058,1126,,12/13,Jul 1- 2012,e-frankfurt,24,1-fc-koln,3,£1.06m,Free transfer
581951,1126,,11/12,Jul 1- 2011,fc-st-pauli,35,eintracht-frankfurt,24,£1.70m,£425k
295000,1126,,09/10,Jul 1- 2009,alem-aachen,8,fc-st-pauli,35,£850k,Free transfer
98459,1126,,06/07,Jul 1- 2006,1860-munich,72,alemannia-aachen,8,£1.36m,£765k
7267,1126,,03/04,Jul 1- 2003,stuttgart-ii,102,tsv-1860-munich,72,-,£21k
...

实际上这些方法适用于我的数据的一部分我的意思是如果我使用相同格式的几条线测试它,但是当涉及到大约40000条记录的原始数据时,它&这些方法不再起作用,对于to_datetime方法,我得到了一个异常,如

ValueError: time data '-' does not match format '%b %d- %Y' (match)

,第二种方法为parse_dates时,日期格式与Jun 11- 2016保持一致。

再次,任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

我认为你需要to_datetime

df.date = pd.to_datetime(df.date, format='%b %d- %Y')
print (df)
   transfer id  player id  player name season       date
0       732058       1126          NaN  12/13 2012-07-01
1       581951       1126          NaN  11/12 2011-06-03
2       295000       1126          NaN  09/10 2009-08-12
3        98459       1126          NaN  06/07 2006-11-06
4         7267       1126          NaN  03/04 2003-07-02

但似乎您可以在read_csv中使用参数parse_dates

import pandas as pd
from pandas.compat import StringIO

temp=u"""transfer id,player id,player name,season,date
732058,1126,,12/13,Jul 1- 2012
581951,1126,,11/12,Jun 3- 2011
295000,1126,,09/10,Aug 12- 2009
98459,1126,,06/07,Nov 6- 2006
7267,1126,,03/04,Jul 2- 2003
"""
#after testing replace StringIO(temp) to filename
df = pd.read_csv(StringIO(temp), parse_dates=['date'])

print (df)
   transfer id  player id  player name season       date
0       732058       1126          NaN  12/13 2012-07-01
1       581951       1126          NaN  11/12 2011-06-03
2       295000       1126          NaN  09/10 2009-08-12
3        98459       1126          NaN  06/07 2006-11-06
4         7267       1126          NaN  03/04 2003-07-02

通过评论编辑:

您需要参数errors='coerce'来替换错误数据(格式与NaT不匹配):

df.date = pd.to_datetime(df.date, format='%b %d- %Y', errors='coerce')

print (df)
   transfer id  player id  player name season         date     move from  \
0       732058       1126          NaN  12/13  Jul 1- 2012   e-frankfurt   
1       581951       1126          NaN  11/12  Jul 1- 2011   fc-st-pauli   
2       295000       1126          NaN  09/10  Jul 1- 2009   alem-aachen   
3        98459       1126          NaN  06/07  Jul 1- 2006   1860-munich   
4         7267       1126          NaN  03/04  Jul 1- 2003  stuttgart-ii   
5         7267       1126          NaN  03/04            -  stuttgart-ii   

   move from id              move to  move to id market value   transfer fee  
0            24            1-fc-koln           3       £1.06m  Free transfer  
1            35  eintracht-frankfurt          24       £1.70m          £425k  
2             8          fc-st-pauli          35        £850k  Free transfer  
3            72     alemannia-aachen           8       £1.36m          £765k  
4           102      tsv-1860-munich          72            -           £21k  
5           102      tsv-1860-munich          72            -           £21k 
    
df.date = pd.to_datetime(df.date, format='%b %d- %Y', errors='coerce')
print (df)
   transfer id  player id  player name season       date     move from  \
0       732058       1126          NaN  12/13 2012-07-01   e-frankfurt   
1       581951       1126          NaN  11/12 2011-07-01   fc-st-pauli   
2       295000       1126          NaN  09/10 2009-07-01   alem-aachen   
3        98459       1126          NaN  06/07 2006-07-01   1860-munich   
4         7267       1126          NaN  03/04 2003-07-01  stuttgart-ii   
5         7267       1126          NaN  03/04        NaT  stuttgart-ii   

   move from id              move to  move to id market value   transfer fee  
0            24            1-fc-koln           3       £1.06m  Free transfer  
1            35  eintracht-frankfurt          24       £1.70m          £425k  
2             8          fc-st-pauli          35        £850k  Free transfer  
3            72     alemannia-aachen           8       £1.36m          £765k  
4           102      tsv-1860-munich          72            -           £21k  
5           102      tsv-1860-munich          72            -           £21k  
相关问题