user229519
user229519

Reputation: 73

concatenating data frames python

I need to concatenate 6 data frames and csv file names are stored as 'all_files'. I have the following code:

df_from_each_file = (pd.read_csv(f, encoding = 'utf-8') for f in all_files)
data = pd.concat(df_from_each_file, ignore_index=True)

It gives the following error:

First line works well, but second line gives this error:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf6 in position 1: invalid start byte

I tried different encodings but still gives the error. Do you guys have any idea?

Upvotes: 0

Views: 134

Answers (2)

WhiteHat
WhiteHat

Reputation: 120

Maybe it's the separator, try either of these:

pd.read_csv(f,sep = ';')
pd.read_csv(f,sep = ',')

Upvotes: 0

razimbres
razimbres

Reputation: 5015

@user229519, use encoding='latin-1' for pd.read_csv and also axis=0 or 1 for pd.concat

Upvotes: 1

Related Questions