【问题标题】:How to merge or concatenate two different datasets into one如何将两个不同的数据集合并或连接成一个
【发布时间】:2021-09-29 00:44:45
【问题描述】:

我有一个带有代码的字符串列表

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

我知道这行不通。我不知道将两个数据集合并为一个具有公共时间列的其他方法。 我会很感激你的提示 谢谢

【问题讨论】:

  • 这能回答你的问题吗? Pandas Merging 101
  • 不是真的,因为我有一个包含 50 个字符串的列表,它们不是单独的变量,每个都带有单独的信息

标签: python pandas merge concatenation


【解决方案1】:

你可以这样做

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

【讨论】:

  • 我没有变量名 df_aa 和 df_gme
  • 我只有一个字符串列表
  • 你能推荐一种使用 concat 的方法吗?
  • 对不起,我没有提到我有 50 个代码它不仅仅是其中的 2 个
  • 它只适用于两个代码而不是 50
猜你喜欢
  • 1970-01-01
  • 2021-07-09
  • 1970-01-01
  • 2015-06-25
  • 1970-01-01
  • 1970-01-01
  • 2012-01-01
  • 1970-01-01
  • 2018-10-02
相关资源
最近更新 更多