Pandas 如何跳过具有独立列的多个CSV文件中的重复标题,并合并为一个大数据帧

Pandas 如何跳过具有独立列的多个CSV文件中的重复标题,并合并为一个大数据帧,pandas,Pandas,我复制了34个CSV文件,这些文件在google colab中具有相同的列,并试图合并为一个大数据帧。但是,每个CSV都有一个重复的标头,需要跳过该标头 连接时将跳过实际标题,因为具有相同列的CSV文件是否正确 dfs = [pd.read_csv(path.join('/content/drive/My Drive/',x)skiprows=1) for x in os.listdir('/content/drive/My Drive/') if path.isfile(path.join('

我复制了34个CSV文件,这些文件在google colab中具有相同的列,并试图合并为一个大数据帧。但是,每个CSV都有一个重复的标头,需要跳过该标头

连接时将跳过实际标题,因为具有相同列的CSV文件是否正确

dfs = [pd.read_csv(path.join('/content/drive/My Drive/',x)skiprows=1) for x in os.listdir('/content/drive/My Drive/') if path.isfile(path.join('/content/drive/My Drive/',x))]
df = pd.concat(dfs)
上面的代码抛出下面的错误

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe2 in position 1: invalid continuation byte
下面的代码适用于示例文件,但需要一种有效的方法来跳过dup头并合并到一个数据帧中。请建议

df1=pd.read_csv("./Aug_0816.csv",skiprows=1)
df2=pd.read_csv("./Sep_0916.csv",skiprows=1)
df3=pd.read_csv("./Oct_1016.csv",skiprows=1)
df4=pd.read_csv("./Nov_1116.csv",skiprows=1)
df5=pd.read_csv("./Dec_1216.csv",skiprows=1)
dfs=[df1,df2,df3,df4,df5]
df=pd.concat(dfs)

您是否考虑过使用标准库中的
glob

试试这个

path = ('/content/drive/My Drive/')
os.chdir(path)

allFiles = glob.glob("*.csv")
dfs = [pd.read_csv(f,header=None,error_bad_lines=False) for f in allFiles]
#or if you know the specific delimiter for your csv 
#dfs = [pd.read_csv(f,header=None,delimiter='yourdelimiter') for f in allFiles]
df = pd.concat(dfs)

您是否考虑过使用标准库中的
glob

试试这个

path = ('/content/drive/My Drive/')
os.chdir(path)

allFiles = glob.glob("*.csv")
dfs = [pd.read_csv(f,header=None,error_bad_lines=False) for f in allFiles]
#or if you know the specific delimiter for your csv 
#dfs = [pd.read_csv(f,header=None,delimiter='yourdelimiter') for f in allFiles]
df = pd.concat(dfs)

试试这个,这是用通用文件名格式在特定路径中连接多个“n”csv文件的最通用脚本

def get_merged_csv(flist, **kwargs):
    return pd.concat([pd.read_csv(f,**kwargs) for f in flist], ignore_index=True)

path = r"C:\Users\Jyotsna\Documents"
fmask = os.path.join(path, 'Detail**.csv')

df = get_merged_csv(glob.glob(fmask), index_col=None)
df.head()
如果要在连接之前跳过每个文件中的某些固定行和/或列,请相应地编辑此行中的代码

return pd.concat([pd.read_csv(f, skiprows=4,usecols=range(9),**kwargs) for f in flist], ignore_index=True)

试试这个,这是用通用文件名格式在特定路径中连接多个“n”csv文件的最通用脚本

def get_merged_csv(flist, **kwargs):
    return pd.concat([pd.read_csv(f,**kwargs) for f in flist], ignore_index=True)

path = r"C:\Users\Jyotsna\Documents"
fmask = os.path.join(path, 'Detail**.csv')

df = get_merged_csv(glob.glob(fmask), index_col=None)
df.head()
如果要在连接之前跳过每个文件中的某些固定行和/或列,请相应地编辑此行中的代码

return pd.concat([pd.read_csv(f, skiprows=4,usecols=range(9),**kwargs) for f in flist], ignore_index=True)

您的代码是否跳过dup头?刚刚尝试使用代码获取`ParserError:Error标记化数据。C错误:第20657行预期有20个字段,saw 35'。看起来是dup头导致了问题?@Optimizer您需要将
skiprows=[1]
参数也添加到DataNoveler的代码中。我不认为是dup头,可能是您的csv大小不同。当您
打印(所有文件)
时,它是否只列出要合并的CSV?您还可以传递
headers=None
@datanoveler,不存在类似下面这样的复制头。我通过添加skiprows=[1]并获得标记化错误来尝试使用您的代码Zone State Depot Pincode Store Code Store Language Zone State Depot Pincode Store Code Store Language``path=('/content/drive/My drive/Nippon Data/')os.chdir(path)allFiles=glob.glob(“*.csv”)dfs=[pd.read_csv(f,skiprows=[1])for f in allFiles]df=pd.concat(dfs)parserror:错误标记化数据。C错误:第41539行中预期有20个字段,saw 34`@DataNearoor它工作了,跳过了一系列带有无序行号的错误记录,如何检查错误的确切内容?`Ex:b'跳过第41539行:预期有20个字段,saw 34“`您的代码是否跳过dup头?刚刚尝试使用代码获取`ParserError:Error标记化数据。C错误:第20657行预期有20个字段,saw 35'。看起来是dup头导致了问题?@Optimizer您需要将
skiprows=[1]
参数也添加到DataNoveler的代码中。我不认为是dup头,可能是您的csv大小不同。当您
打印(所有文件)
时,它是否只列出要合并的CSV?您还可以传递
headers=None
@datanoveler,不存在类似下面这样的复制头。我通过添加skiprows=[1]并获得标记化错误来尝试使用您的代码Zone State Depot Pincode Store Code Store Language Zone State Depot Pincode Store Code Store Language``path=('/content/drive/My drive/Nippon Data/')os.chdir(path)allFiles=glob.glob(“*.csv”)dfs=[pd.read_csv(f,skiprows=[1])for f in allFiles]df=pd.concat(dfs)parserror:错误标记化数据。C错误:第41539行中预期有20个字段,saw 34`@DataNearoor它工作并跳过了一系列带有无序行号的错误记录,如何检查错误的具体内容?`Ex:b'跳过第41539行:预期有20个字段,saw 34'`