【问题标题】:pandas merge two dataframes [duplicate]熊猫合并两个数据框[重复]
【发布时间】:2017-09-04 10:48:26
【问题描述】:

我是熊猫模块的新手。我有一个关于熊猫合并方法的小问题。假设我有两个单独的表,如下:

Original_DataFrame

machine weekNum Percent
 M1        2      75
 M1        5      80
 M1        8      95
 M1       10      90

New_DataFrame

machine weekNum Percent
 M1        1      100
 M1        2      100
 M1        3      100
 M1        4      100
 M1        5      100
 M1        6      100
 M1        7      100
 M1        8      100
 M1        9      100
 M1       10      100

我使用了pandas模块的merge方法,如下:

pd.merge(orig_df, new_df, on='weekNum', how='left')

我得到如下:

    machine    weekNum  Percent_x  Percent_y
 0    M1           2      75         100
 1    M1           5      80         100
 2    M1           8      95         100
 3    M1          10      90         100

但是,我希望填写跳过的 weekNums 并为这些行添加 100 以获得所需的输出,如下所示。

machine weekNum Percent
 M1        1      100
 M1        2      75
 M1        3      100
 M1        4      100
 M1        5      80
 M1        6      100
 M1        7      100
 M1        8      95
 M1        9      100
 M1       10      90

谁能指导我如何进行?

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    我认为您需要combine_first,但首先需要set_index 普通列:

    df11 = df1.set_index(['machine','weekNum'])
    df22 = df2.set_index(['machine','weekNum'])
    
    df = df11.combine_first(df22).astype(int).reset_index()
    print (df)
      machine  weekNum  Percent
    0      M1        1      100
    1      M1        2       75
    2      M1        3      100
    3      M1        4      100
    4      M1        5       80
    5      M1        6      100
    6      M1        7      100
    7      M1        8       95
    8      M1        9      100
    9      M1       10       90
    
    
    df.plot.bar('weekNum', 'Percent')
    

    编辑:

    对于标签:

    plt.figure(figsize=(12, 8))
    ax = df.plot.bar('weekNum', 'Percent')
    rects = ax.patches
    
    for rect, label in zip(rects, df['Percent']):
        height = rect.get_height()
        ax.text(rect.get_x() + rect.get_width()/2, height + 1, label, ha='center', va='bottom')
    
    plt.ylim(ymax=120)
    

    【讨论】:

    • 在运行倒数第二个代码后给我一个错误如下: ValueError: invalid literal for int() with base 10: 'M1'
    • 对不起,我在第一个版本的代码中有错字。需要df11df22 - df = df11.combine_first(df22).astype(int).reset_index()
    • 还是同样的错误。 ValueError: int() 以 10 为底的无效文字:'M1' :(
    • 你想要this - 标签吗?
    • 赞!!!!非常感谢,jezrael...帮助很大!!!
    【解决方案2】:

    不像其他解决方案那样优雅,但仍然有效:

    # join
    merged = pd.merge(data1, data2, on=['machine','weekNum'], how='outer')
    # combine percent columns
    merged['Percent'] = merged['Percent_x'].fillna(merged['Percent_y'])
    # remove extra columns
    result = merged[['machine','weekNum', 'Percent']]
    

    结果:

    machine weekNum Percent
    M1  2   75
    M1  5   80
    M1  8   95
    M1  10  90
    M1  1   100
    M1  3   100
    M1  4   100
    M1  6   100
    M1  7   100
    M1  9   100
    

    【讨论】:

    • 没错,但是我想用原始数据覆盖weekNumbers 2,5,8和10的记录。
    • 有效!谢谢derline
    【解决方案3】:

    你可以试试这个。根据您的总体目标,这可能不够“程序化”。

    import pandas as pd    
    df1 = pd.DataFrame({"machine":["M1"]*4, "WeekNum": [2,5,8,10], "Percent":[75,80,95,90]})
    df2 = pd.DataFrame({"machine":["M1"]*10,"WeekNum":np.arange(1,11,1),"Percent":[100]*10})
    newcol = df2.merge(df1, on = "WeekNum", how = "outer")["Percent_y"].fillna(100)
    df2["Percent"] = newcol
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-28
      • 2017-11-13
      • 2017-11-26
      • 1970-01-01
      • 2017-06-11
      • 2016-01-01
      相关资源
      最近更新 更多