【问题标题】:Merge values in next row pandas合并下一行熊猫中的值
【发布时间】:2019-03-27 12:07:52
【问题描述】:

我正在尝试合并两个数据框:

this are tables I am trying to merge

and this is the result I want to achieve

所以,我希望合并的值不在同一行。

当我尝试这样合并时:

pd.merge(a,b,how='inner', on=['date_install','device_os'])

它将值合并为一个原始值。

请提出解决方法

【问题讨论】:

  • 那么需要df = pd.concat([a, b], ignore_index=True) 吗?
  • 如果是,就是骗this

标签: python pandas


【解决方案1】:

你可以使用append():

df1.append(df2, ignore_index = True)

或使用concat():

pd.concat([df,df1], ignore_index = True)

或使用merge():

df.merge(df1, how = 'outer')

【讨论】:

    【解决方案2】:

    参考来自:https://stackoverflow.com/a/24391268/3748167(可能重复)

    这里唯一的区别是首先连接两个数据帧,然后应用其余的逻辑。

    def sjoin(x): return ';'.join(x[x.notnull()].astype(str)) pd.concat([a,b]).groupby(level=0, axis=1).apply(lambda x: x.apply(sjoin, axis=1))

    【讨论】:

      猜你喜欢
      • 2018-05-07
      • 2023-02-05
      • 2017-04-17
      • 2020-11-11
      • 2023-01-23
      • 2022-12-06
      • 1970-01-01
      • 1970-01-01
      • 2018-09-05
      相关资源
      最近更新 更多