【问题标题】:how to append elements in a list in dataframe to elements in a list in another dataframe?如何将数据框中列表中的元素附加到另一个数据框中列表中的元素?
【发布时间】:2022-07-31 17:53:44
【问题描述】:

我想将 df1 中的列的值附加到 df2 中的列的值,并且与“b”列相同。结果应该是:

【问题讨论】:

  • 请始终以文本(而不是图像)的形式提供数据。

标签: python pandas dataframe


【解决方案1】:

为此使用pd.combine

df1 = pd.DataFrame({
    'a' : [[*range(5)]],
    'b' : [[*range(5,10)]]
})
df2 = pd.DataFrame({
    'a' : [[*range(11,14)]],
    'b' : [[*range(22,25)]]
})

result = df1.combine(df2, lambda x, y: x + y)

输出result:

                             a                            b
0  [0, 1, 2, 3, 4, 11, 12, 13]  [5, 6, 7, 8, 9, 22, 23, 24]

【讨论】:

    猜你喜欢
    • 2020-05-22
    • 2019-11-06
    • 1970-01-01
    • 1970-01-01
    • 2018-09-07
    • 1970-01-01
    • 2022-08-04
    • 2019-04-01
    • 1970-01-01
    相关资源
    最近更新 更多