我有一个像这样的csv文件:
"NoDemande;"NoUsager";"Sens";"IdVehicule";"NoConducteur";"NoAdresse";"Fait";"aPaye";"MethodePaiement";"ArgentPercu";"HeurePrevue";"HeureDebutTrajet";"HeureArriveeSurSite";"HeureEffective"
0003;"2021";"+";"157Véh";"0002";"5712";"1";"";"";"";"07/07/2015 06:30:04";"07/07/2015 06:15:48";"07/07/2015 06:32:14";"07/07/2015 06:32:23"
0265;"0496";"+";"161Véh";"0035";"04075";"1";"";"";"";"07/07/2015 06:35:04";"07/07/2015 05:09:55";"07/07/2015 06:36:18";"07/07/2015 06:36:27"
0004;"2208";"+";"157Véh";"0002";"5713";"1";"";"";"";"07/07/2015 06:45:04";"07/07/2015 06:32:23";"07/07/2015 06:40:05";"07/07/2015 06:40:10"
我想做什么:
我试过这样:
#coding=latin-1
import pandas as pd
import glob
pd.set_option('expand_frame_repr', False)
path = r'D:\Python27\mypfe\data_test'
allFiles = glob.glob(path + "/*.csv")
frame = pd.DataFrame()
list_ = []
for file_ in allFiles:
df = pd.read_csv(file_,index_col=None,header=0,sep=';')
s1 = df['HeurePrevue'].apply(lambda x: x.split(' '))
df['Date'] = s1.apply(lambda x: x[0])
df['HeurePrevue'] = s1.apply(lambda x: x[1])
frame = pd.concat(list_)
print frame
它适用于一列,但是当我想要对第二列执行相同操作时,它不再有效。我该怎么做才能让它与我的所有列一起使用,同时保存其他列?
答案 0 :(得分:0)
import io
import pandas as pd
raw_df = io.StringIO("""\
HeurePrevue HeureDebutTrajet HeureArriveeSurSite HeureEffective
06/07/2015 05:30:04 06/07/2015 16:54:31 06/07/2015 16:54:35 06/07/2015 16:54:38
06/07/2015 06:10:04 06/07/2015 05:38:39 06/07/2015 06:29:51 06/07/2015 06:30:06
06/07/2015 06:10:04 06/07/2015 05:38:39 06/07/2015 06:29:51 06/07/2015 06:30:06
""")
df = pd.read_csv(raw_df, index_col=None, skiprows=1, header=None,
delim_whitespace=True)
df = df[[0, 1, 3, 5, 7]]
df.columns = ['Date', 'HeurePrevue', 'HeureDebutTrajet', 'HeureArriveeSurSite',
'HeureEffective']
输出:
Date HeurePrevue HeureDebutTrajet HeureArriveeSurSite HeureEffective
0 06/07/2015 05:30:04 16:54:31 16:54:35 16:54:38
1 06/07/2015 06:10:04 05:38:39 06:29:51 06:30:06
2 06/07/2015 06:10:04 05:38:39 06:29:51 06:30:06