简体   繁体   English

Pandas:如何合并具有相同列的多个数据帧?

[英]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.经过大量的尝试和错误并在论坛上搜索(包括 StackOverflow Similar Question ),我终于来这里问这个问题了。 Let me quickly explain what I am trying to achieve.让我快速解释一下我想要实现的目标。

I have daily data on options contracts.我有期权合约的每日数据。 Data is stored in CSV format;数据以CSV格式存储; Every day have one CSV file.每天都有一个 CSV 文件。

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.我们的操作:在日期和时间上将数据并排添加到 Dataframe。

How our DF should look:我们的 DF 应该是什么样子:

---------------- 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)尝试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请确保raw_data dataframe 应该有 datetime.index

ref link- Calculating RSI using "finta", getting error参考链接-使用“finta”计算 RSI,出现错误

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

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