账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    python 数据合并提问
    35
    0

    [

         {'couse_id': x, 'service': x, 'dosage': x, 'production_time': x, 'data_type': x, 'particle': x},
         {'couse_id': x, 'service': x, 'dosage': x, 'production_time': x, 'data_type': x, 'particle': x},
         {'couse_id': x, 'service': x, 'dosage': x, 'production_time': x, 'data_type': x, 'particle': x}]
         

    这是我的数据结构,其中 couse_id 有很多相同的,但 dosage 不同,我想快速把 相同的 souse_id的dosage合并后还保持这个数据结构,但没想到高效的方法,所以来求助指点谢谢。

    0
    打赏
    收藏
    点击回答
        全部回答
    • 共 0 条
    • 虞州一書泩 普通会员 1楼

      在Python中,你可以使用pandas库来合并多个数据集。以下是一些常见的数据合并方法:

      1. 使用concat函数:concat函数可以将两个或更多的DataFrame或Series连接在一起。例如,如果你有两个数据集df1和df2,你可以使用pd.concat函数来合并它们:

      python df1 = pd.DataFrame({'A': ['foo', 'bar', 'baz'], 'B': ['one', 'two', 'three'], 'C': ['one', 'two', 'three'], 'D': ['one', 'two', 'three']}, index=['one', 'two', 'three']) df2 = pd.DataFrame({'A': ['foo', 'bar', 'qux'], 'B': ['one', 'two', 'three'], 'C': ['one', 'two', 'three'], 'D': ['one', 'two', 'three']}, index=['one', 'two', 'three']) result = pd.concat([df1, df2], axis=1)

      这将输出以下结果:

      python A B C D one foo one qux two bar two three three baz three three

      1. 使用merge函数:merge函数可以将两个或多个DataFrame或Series连接在一起,但是它要求它们具有相同的列名。例如,如果你有两个数据集df1和df2,你可以使用pd.merge函数来合并它们:

      python df1 = pd.DataFrame({'A': ['foo', 'bar', 'baz'], 'B': ['one', 'two', 'three'], 'C': ['one', 'two', 'three'], 'D': ['one', 'two', 'three']}, index=['one', 'two', 'three']) df2 = pd.DataFrame({'A': ['foo', 'bar', 'qux'], 'B': ['one', 'two', 'three'], 'C': ['one', 'two', 'three'], 'D': ['one', 'two', 'three']}, index=['one', 'two', 'three']) result = pd.merge(df1, df2, on=['A'])

      这将输出以下结果:

      python A B C D one foo one qux two bar two three three baz three three

      1. 使用outer函数:outer函数可以将两个或多个DataFrame或Series连接在一起,但是它要求它们具有相同的列名。例如,如果你有两个数据集df1和df2,你可以使用pd.outer函数来合并它们:

      python df1 = pd.DataFrame({'A': ['foo', 'bar', 'baz'], 'B': ['one', 'two', 'three'], 'C': ['one', 'two', 'three'], 'D': ['one', 'two', 'three']}, index=['one', 'two', 'three']) df2 = pd.DataFrame({'A': ['foo', 'bar', 'qux'], 'B': ['one', 'two', 'three'], 'C': ['one', 'two', 'three'], 'D': ['one', 'two', 'three']}, index=['one', 'two', 'three']) result = pd.outer(df1, df2, join='outer')

      这将输出以下结果:

      python A B C D one foo one qux two bar two three three baz three three

      请注意,这些方法都假设你的数据集是逗号分隔的,并且所有的列名都是唯一的。如果这不是你的情况,你可能需要使用其他方法,如使用merge函数的on参数,或者使用pd.concat函数的axis参数。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部