简体   繁体   English

使用python将多个具有相同标题但不同csv文件名的CSV文件合并为一个文件

[英]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... .我是 python 新手……我尝试应用此代码来合并多个 csv 文件,但它不起作用……基本上,我有一个文件,其中包含带有标题的股票价格:日期、开盘价、高价、低价, 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:但每个 csv 文件都有不同的名称:Apl.csv、VIX.csv、FCHI.csv 等。我想将所有这些 csv 文件合并为一个 .. 但我想添加一个新列来公开名称csv 文件示例:

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: stock_id,date,open,High,low,Close,Adj Close Volume with stock_id = apl,Vix 等。

  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 ?有人可以帮我解决这个问题吗?

In Python, the indentation level matters, and you need a colon at the end of an if statement.在 Python 中,缩进级别很重要,并且在 if 语句的末尾需要一个冒号。 I can't speak to the method you're trying, but you can clean up the synax with this:我不能说你正在尝试的方法,但你可以用这个清理synax:

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")

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 :我在 python 中相对较新..这是我想要做的..我有一个包含多个 csv 文件的文件夹(2018.csv、2017.csv、2016.csv 等)500 个 csv 文件是精确的.. 每个 csv 包含标题“日期”、“代码”、“Cur”、价格等。我想将所有 500 个 csv 文件连接到一个 datafame 中……这是我的一个 csv 文件的代码,但速度很慢,我想对所有 500 个文件执行此操作,并在一个数据框中进行 concantanate:

 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(',',';')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM