pandas 合并两个数据框 [英] pandas merge two dataframes

查看:77
本文介绍了 pandas 合并两个数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



Original_DataFrame

 机器周数量百分比
M1 2 75
M1 5 80
M1 8 95
M1 10 90

New_DataFrame

 机器周百分比
M1 1100
M1 2100
M1 3100
M1 4100
M1 5100
M1 6100
M1 7 100
M1 8100
M1 9100
M1 10100

我使用了pandas模块的合并方法,如下所示:

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

我得到如下信息:

 机器周Num Percent_x Percent_y 
0 M1 2 75100
1 M1 5 80100
2 M1 8 95100
3 M1 10 90100

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

 机器周数值百分比
M1 1100
M1 2 75
M1 3100
M1 4100
M1 5 80
M1 6100
M1 7100
M1 8 95
M1 9100
M1 10 90

有人可以指导我如何进行吗?

解决方案

我认为您需要



编辑:



对于标签:

  plt.figure(figsize =(12,8))
ax = df.plot.bar('weekNum','Percent')
rects = ax.patches

为rect,在zip中标记(rects,df ['Percent']):
高度= rect.get_height()
ax.text(rect.get_x()+ rect.get_width ()/ 2,高度+ 1,标签,ha ='center',va ='bottom')

plt.ylim(ymax = 120)


I am new to pandas module. I have a small question regarding pandas merge method. Suppose I have two separate tables, as follows:

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

I used merge method of pandas module, as follows:

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

I get as follows:

    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

However, I am looking to fill up the skipped weekNums and put 100 for those rows to get the desired output as follows.

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

Can anyone please direct me how to proceed?

解决方案

I think you need combine_first, but first set_index by common columns:

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')

EDIT:

For labels:

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)

这篇关于 pandas 合并两个数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆