Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用pandas合并两个数据帧_Python_Pandas_Dataframe_Merge - Fatal编程技术网

Python 使用pandas合并两个数据帧

Python 使用pandas合并两个数据帧,python,pandas,dataframe,merge,Python,Pandas,Dataframe,Merge,这是数据帧的子集 F1: 我需要比较F1.id和F2.id,然后将列id中的差异添加到F2数据框中,并将添加id的列值填入0 这是第二个数据帧 F2: 输出应如下所示: F3: 我尝试了不同的解决方案,例如 F1[F1.index.isinF2.index&F1.isinF2]返回差异,但其中没有一个有效 使用外部合并+填充: 使用外部合并+填充: 通过使用reindex 通过使用reindex 开箱即用 具有正常索引 i = np.setdiff1d(F1.id, F2.id) F2.appe

这是数据帧的子集 F1:

我需要比较F1.id和F2.id,然后将列id中的差异添加到F2数据框中,并将添加id的列值填入0

这是第二个数据帧 F2:

输出应如下所示:

F3:

我尝试了不同的解决方案,例如 F1[F1.index.isinF2.index&F1.isinF2]返回差异,但其中没有一个有效

使用外部合并+填充:

使用外部合并+填充:

通过使用reindex

通过使用reindex

开箱即用

具有正常索引

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
开箱即用

具有正常索引

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


检查您的预期输出。。。里面有一个13。这是一个错误吗?你是如何得到两个h.1..在列ID中检查你的预期输出。。。里面有一个13。这是个错误吗?你怎么会在专栏里得到两个h.1Id@COLDSPEED非常感谢。还有什么不提df2中列的名称的吗?我在df2.astypedf2.dttypespirsquared中有大约40列,非常好。我不知道你能做到@玛丽,你有答案了。@COLDSPEED,谢谢你。还有什么不提df2中列的名称的吗?我在df2.astypedf2.dttypespirsquared中有大约40列,非常好。我不知道你能做到@玛丽,你有你的答案。很好。您可以缩短它:df2.set_index'id'.reindexdf['id'],fill_value=0.reset_index-bonus:您可以获得int列,因此不需要转换。它还保留dtypes@cᴏʟᴅsᴘᴇᴇᴅ 是的,你说得对:好多了!顺便说一句…我不认为我得到了他想要的预期结果…这和我的一样吗?OP似乎赞成它。@cᴏʟᴅsᴘᴇᴇᴅ 根据他的描述,我们的解决方案应该是可以的,但如果你看看他预期的产量……非常好。您可以缩短它:df2.set_index'id'.reindexdf['id'],fill_value=0.reset_index-bonus:您可以获得int列,因此不需要转换。它还保留dtypes@cᴏʟᴅsᴘᴇᴇᴅ 是的,你说得对:好多了!顺便说一句…我不认为我得到了他想要的预期结果…这和我的一样吗?OP似乎赞成它。@cᴏʟᴅsᴘᴇᴇᴅ 根据他的描述,我们的解决方案应该是可以的,但如果你看看他预期的产量。。。。
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
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
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
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
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