簡體   English   中英

Pandas:如何合並具有相同列的多個數據幀?

[英]Pandas : How to merge multiple dataframes with same coloumns?

經過大量的嘗試和錯誤並在論壇上搜索(包括 StackOverflow Similar Question ),我終於來這里問這個問題了。 讓我快速解釋一下我想要實現的目標。

我有期權合約的每日數據。 數據以CSV格式存儲; 每天都有一個 CSV 文件。

數據格式:

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

我們的操作:在日期和時間上將數據並排添加到 Dataframe。

我們的 DF 應該是什么樣子:

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

我們正在執行合並操作的示例代碼

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


我收到以下錯誤:

reindexing only valid with uniquely valued index objects

我的問題:我們應該如何合並具有相同列的多個數據集?

我應該如何解決我面臨的錯誤?

嘗試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)

請確保raw_data dataframe 應該有 datetime.index

參考鏈接-使用“finta”計算 RSI,出現錯誤

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM