Pandas在合并多个CSV文件后为输出文件添加标题

0 人关注
import pandas as pd
import os
file1 = 'https://public.fyers.in/sym_details/NSE_CM.csv'
file2 = 'https://public.fyers.in/sym_details/NSE_FO.csv'
file3 = 'https://public.fyers.in/sym_details/BSE_CM.csv'
CHUNK_SIZE = 10 ** 6
csv_file_list = [file1, file2, file3]
output_file = "/content/output.csv"
for csv_file_name in csv_file_list:
  skipRows = [2022,92805]
  chunk_container = pd.read_csv(csv_file_name, chunksize=CHUNK_SIZE, skiprows=skipRows)
  for chunk in chunk_container:
    headerList =["fytoken", "symbol", "instrumentType","lotSize","tickSize","ISIN","tradingSession","lastUpdate","expiryDate","symbolTicker","exchange","segment","scripCode","scripName","scripToken","strikePrice","optionType"]
    chunk.to_csv(output_file,header=headerList, mode="a", index=False)

我想合并这三个CSV文件,并在输出文件中添加标题。 但它返回的输出文件在每个CSV的开头都有标题(在输出文件中)。

2 个评论
就像文件被合并了,但它有标题,你不希望这样。对吗?
你正在分块读取文件,并为每个分块添加 header
python
pandas
csv
Siddharth Mankar
Siddharth Mankar
发布于 2022-07-07
1 个回答
Rohit Babu
Rohit Babu
发布于 2022-07-07
已采纳
0 人赞同

你正在分块阅读内容,并为每块内容附加 header

相反,请尝试以下。

import pandas as pd
file1 = 'https://public.fyers.in/sym_details/NSE_CM.csv'
file2 = 'https://public.fyers.in/sym_details/NSE_FO.csv'
file3 = 'https://public.fyers.in/sym_details/BSE_CM.csv'
CHUNK_SIZE = 10 ** 6
csv_file_list = [file1, file2, file3]
output_file = "./content/output.csv"
headerList = ["fytoken", "symbol", "instrumentType", "lotSize", "tickSize", "ISIN", "tradingSession",
              "lastUpdate", "expiryDate", "symbolTicker", "exchange", "segment", "scripCode", "scripName",
              "scripToken", "strikePrice", "optionType"]
df = pd.DataFrame(columns=headerList)
df.to_csv(output_file, index=False)
for csv_file_name in csv_file_list:
    skipRows = [2022, 92805]