zoukankan      html  css  js  c++  java
  • pandas 获取增量数据

    场景: 每次从日志中拿到全量数据,然后存放到数据库中,那么如何得到上一次与这一次的增量数据呢?
    比如上一次数据是

    df2 = DataFrame([['a', '男', 10],
    ['b', '男', 11],
    ['c', '女', 11]],
    columns=['name', 'sex', 'age'])
    这一次数据是
    df1 = DataFrame([['a', '男', 20],

    ['b', '男', 15],
                     ['c', '女', 14],
    ['a', '女', 10],
    ['c', '男', 11]],
    columns=['name', 'sex', 'money'])

    想得到df4:

    name sex age

    0 a 女 10.0
    1 a 男 10.0
    2 b 男 4.0
    3 c 女 3.0
    4 c 男 11.0

     

      主要用到combine_first函数

    __version__ = '1.0.0.0'
    """
    @brief : 简介
    @details: 详细信息
    @author : zhphuang
    @date : 2018-10-29
    """
    import pandas as pd
    import random
    from pandas import *
    df1 = DataFrame([['a', '男', 20],
    ['b', '男', 15],
    ['c', '女', 14],
    ['a', '女', 10],
    ['c', '男', 11]],
    columns=['name', 'sex', 'age'])

    df2 = DataFrame([['a', '男', 10],
    ['b', '男', 11],
    ['c', '女', 11]],
    columns=['name', 'sex', 'age'])

    df1.set_index(['name', 'sex'], inplace=True)
    print("df1: %s " % df1)
    df2.set_index(['name', 'sex'], inplace=True)
    print("df2: %s " % df2)
    df4 = df1 - df2
    # df3.reset_index(['name', 'sex'], inplace=True)
    # df4.reset_index(['name', 'sex'], inplace=True)
    print("df4: %s " % df4)

    df4 = df4.combine_first(df1)
    df4.reset_index(['name', 'sex'], inplace=True)
    print("df4: %s " % df4)
    当值未一旬,而视茫茫,而发苍苍,而齿牙动摇
  • 相关阅读:
    hdu 1258 DFS
    hdu2488 dfs
    poj1915 BFS
    hdu1372 BFS求最短路径长度
    poj3264 线段树
    hdu 2438Turn the corner 三分
    hdu3714 三分
    【转载】单点登陆
    ajax从入门到深入精通
    Web前端技术体系大全搜索
  • 原文地址:https://www.cnblogs.com/niuniuc/p/11271725.html
Copyright © 2011-2022 走看看