KIKI99
KIKI99

Reputation: 1

merging multiple CSV files in one with same header but different csv files name with python

I'm new in python ...I have tried to apply this code to merge multiple csv files but it doesn't work..basically, I have a files which contains stock prices with header: date,open,High,low,Close,Adj Close Volume... . but each csv file has a different name: Apl.csv,VIX.csv,FCHI.csv etc.. I would like to merge all these csv files in One.. but I would like to add a new columns which will disclose the name of the csv files example:

stock_id,date,open,High,low,Close,Adj Close Volume with stock_id = apl,Vix etc.. I used this code but I got stuck in line 4 here is the code:

  files = os.listdir() 
  file_list = list() 
  for file in os.listdir():
      if file.endswith(".csv")
      df=pd.read_csv(file,sep=";")
      df['filename'] = file
      file_list.append(df) 
  all_days = pd.concat(file_list, axis=0, ignore_index=True) 
  all_days.to_csv("all.csv")

Someone could help me to sort out this ?

Upvotes: 0

Views: 1235

Answers (2)

KIKI99
KIKI99

Reputation: 1

I'm relatively new in python ..here is what I'd like to do..I got a folder with multiples csv files ( 2018.csv,2017.csv,2016.csv etc..)500 csv files to be precise.. each csv contains header "date","Code","Cur",Price etc..I'd like to concatenate all 500 csv files in one datafame...here is my code for one csv files but it's very slow , I want to do it for all 500 files and concantanate in one dataframe :

 DB_2017 = pd.read_csv("C:/folder/2018.dat",sep=",", header =None).iloc[: 0,4,5,6]

 DB_2017.columns =["date","Code","Cur",Price]

 DB_2017['Code'] =DB_2017['Code'].map(lambdax:x.lstrip('@').rstrip('@'))

 DB_2017['Cur'] =DB_2017['Cur'].map(lambdax:x.lstrip('@').rstrip('@'))

 DB_2017['date'] =DB_2017['date'].apply(lambdax:pd.timestamp(str(x)[:10)

 DB_2017['Price'] =pd.to_numeric(DB_2017.Price.replace(',',';')

Upvotes: 0

sjc
sjc

Reputation: 1137

In Python, the indentation level matters, and you need a colon at the end of an if statement. I can't speak to the method you're trying, but you can clean up the synax with this:

files = os.listdir() 
file_list = list() 
for file in os.listdir():
    if file.endswith(".csv"):
        df=pd.read_csv(file,sep=";")
        df['filename'] = file
        file_list.append(df) 
all_days = pd.concat(file_list, axis=0, ignore_index=True) 
all_days.to_csv("all.csv")

Upvotes: 0

Related Questions