我试图用python读取多个csv
文件。原始数据的索引(或第一列)有点问题,部分csv文件如下:
NoDemande;"NoUsager";"Sens";"IdVehiculeUtilise";"NoConducteur";"NoAdresse";"Fait";"HeurePrevue"
42210000003;"42210000529";"+";"265Véh";"42210000032";"42210002932";"1";"25/07/2015 10:00:04"
42210000005;"42210001805";"+";"265Véh";"42210000032";"42210002932";"1";"25/07/2015 10:00:04"
42210000004;"42210002678";"+";"265Véh";"42210000032";"42210002932";"1";"25/07/2015 10:00:04"
42210000003;"42210000529";"—";"265Véh";"42210000032";"42210004900";"1";"25/07/2015 10:50:03"
42210000004;"42210002678";"—";"265Véh";"42210000032";"42210007072";"1";"25/07/2015 11:25:03"
42210000005;"42210001805";"—";"265Véh";"42210000032";"42210004236";"1";"25/07/2015 11:40:03"
第一个索引没有""
,在读取文件之后,它看起来是:"NoDemande"
,而其他索引没有{
这会导致在接下来的移动中识别索引名称的问题。如何解决这个问题? 以下是我读取文件的代码:
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:
#Read file
df = pd.read_csv(file_,header=0,sep=';',dayfirst=True,encoding='utf8',
dtype='str')
df['Sens'].replace(u'\u2014','-',inplace=True)
list_.append(df)
print"fichier lu ",file_
frame = pd.concat(list_)
print frame
事实上,我被困在如何从索引中删除双引号。在更改了角度之后,我认为也许最好添加一个新列,从原始列复制值并删除它。因此,新列将具有所需的索引。 就我而言,我做到了:
所以我得到了一个我想要的新的。在
我认为最简单的是设置新的列名:
另一个解决方案是来自列名的^{} 值
^{pr2}$"
:相关问题 更多 >
编程相关推荐