简体   繁体   中英

Pandas : How to merge multiple dataframes with same coloumns?

After a lot of try and error and searching on forums (including StackOverflow Similar Question ), I am finally here to ask this question. Let me quickly explain what I am trying to achieve.

I have daily data on options contracts. Data is stored in CSV format; Every day have one CSV file.

Data Format:

Ticker, Date, Time, Open, High, Low, Close, Volume, Open Interest.

Our Operation: Add data to a Dataframe side-by-side on Date and time.

How our DF should look:

---------------- contract 1 ---------- ------contract 2--------------- [...]
 
Ticker, Date, Time, Open, High,[...]  Ticker, Date, Time, Open, High,[...]

Sample code where we are performing merging operation

# we have stored our data in df. Everyday df gets updated with that day's data.
# The contains options contracts of different companies. We extract company names and ask the user to select one from that list of companies. Then we ask the user to select the start and end date. After that, we iterate for that range and merge dataframes.

raw_date=pd.read_csv("DAily Data")
while start_date < end_date:

   for con in available_options:
      df_options=raw_data[raw_data["Ticker"]=con]
      if df_merged.empty:
          df_merged=df_options
      else:
          df_merged=df_merged.merge(df_options,how="outer",on=["Date","Time"])

   start_date=start_date + day_delay(1) 


I am getting following error:

reindexing only valid with uniquely valued index objects

My questions: How should we merge multiple datasets with the same columns?

How should I resolve the error that I am facing?

Try pd.concat(dataframe, axis=0)

for con in available_options:
      df_options=raw_data[raw_data["Ticker"]=con]
      df_merged = pd.concat(df_options, axis=0)

Pls make sure raw_data dataframe should have datetime.index

ref link- Calculating RSI using "finta", getting error

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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