简体   繁体   English

用熊猫合并两个数据框

[英]Merging two dataframes with pandas

This is a subset of data frame F1: 这是数据帧F1的子集:

id        code    s-code
l.1        1       11
l.2        2       12
l.3        3       13
f.1        4       NA
f.2        3        1
h.1        2        1
h.3        1        1

I need to compare the F1.id with F2.id and then add the differences in column "id" to the F2 data frame and fill in columns' values for the added "id" with 0. 我需要将F1.id与F2.id进行比较,然后将“ id”列中的差异添加到F2数据框中,并为添加的“ id”的列值填充0。

this is the second data frame F2: 这是第二个数据帧F2:

id        head    sweat  pain
l.1        1       0      1
l.3        1       0      0
f.2        3        1     1
h.3        1        1     0

The output should be like this: 输出应如下所示:

F3: F3:

id        head    sweat  pain
l.1        1       0      1
l.3        3       13     0  
f.2        3        1     1
h.1        2        1     1
h.3        1        1     0
l.2        0        0     0
h.1        0        0     0
f.1        0        0     0

I tried different solution, such as F1[(F1.index.isin(F2.index)) & (F1.isin(F2))] to return the differences, but non of them worked. 我尝试了其他解决方案,例如F1[(F1.index.isin(F2.index)) & (F1.isin(F2))]以返回差异,但没有一个有效。

By using reindex 通过使用reindex

df2.set_index('id').reindex(df1.id).fillna(0).reset_index()
Out[371]: 
    id  head  sweat  pain
0  l.1   1.0    0.0   1.0
1  l.2   0.0    0.0   0.0
2  l.3   1.0    0.0   0.0
3  f.1   0.0    0.0   0.0
4  f.2   3.0    1.0   1.0
5  h.1   0.0    0.0   0.0
6  h.3   1.0    1.0   0.0

Use an outer merge + fillna : 使用外部merge + fillna

df[['id']].merge(df2, how='outer')\
            .fillna(0).astype(df2.dtypes)

    id  head  sweat  pain
0  l.1     1      0     1
1  l.2     0      0     0
2  l.3     1      0     0
3  f.1     0      0     0
4  f.2     3      1     1
5  h.1     0      0     0
6  h.3     1      1     0

Outside the Box 在箱子外面

i = np.setdiff1d(F1.id, F2.id)
F2.append(pd.DataFrame(0, range(len(i)), F2.columns).assign(id=i))

    id  head  sweat  pain
0  l.1     1      0     1
1  l.3     1      0     0
2  f.2     3      1     1
3  h.3     1      1     0
0  f.1     0      0     0
1  h.1     0      0     0
2  l.2     0      0     0

With a normal index 具有正常索引

i = np.setdiff1d(F1.id, F2.id)
F2.append(
    pd.DataFrame(0, range(len(i)), F2.columns).assign(id=i),
    ignore_index=True
)

    id  head  sweat  pain
0  l.1     1      0     1
1  l.3     1      0     0
2  f.2     3      1     1
3  h.3     1      1     0
4  f.1     0      0     0
5  h.1     0      0     0
6  l.2     0      0     0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM