[英]How to merge or concatenate two different datasets into one
我有一個帶代碼的字符串列表
tickers = [AA, GME...]
It is just an example I have a variable with 50 tickers
50_tickers = [AA,GME,FB,MSFT...] 50 of them
And I need to combine all of them following the same pattern as below
AA 數據集看起來像這樣
2021:03:21 | 33.45
2021:03:22 | 33.50
2021:03:23 | 33.60
2021:03:24 | 33.70
GME 數據集看起來像這樣
2021:03:21 | 10.45
2021:03:22 | 11.50
2021:03:23 | 12.60
2021:03:24 | 12.65
我想將這兩個數據集組合成這樣
time | price AA | price GME
2021:03:21 | 33.45 | 10.45
2021:03:22 | 33.50 | 11.50
2021:03:23 | 33.60 | 12.60
2021:03:24 | 33.70 | 12.65
這是我嘗試過的
for combine in tickers:
df = pd.DataFrame(combine)
df.concat(df[combine])
我知道這行不通。 我不知道其他方法可以將兩個數據集合並為一個具有公共時間列的數據集。 我會很感激你的提示謝謝
你可以這樣做
df=None
flag=False
for combine in tickers:
if not Flag:
df = pd.DataFrame(combine)
flag=True
else:
df_t = pd.DataFrame(combine)
df = df.merge(df_t, on='time')
df
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.