如何在python中加入或合并

问题描述 投票:0回答:1

我们有两个数据框。我们希望所有列都由Dataframe1组成,但只需要target_name中的一列(Dataframe2)。但是,当我们这样做时,它给我们提供了重复的值。

Dataframe1值

    user_id subject_id  x               y           w           h       g
0   858580  23224814    58.133331   57.466675   181.000000  42.000000   1
1   858580  23224814    293.133331  176.466675  80.000000   34.000000   2
2   313344  28539152    834.049316  37.493195   63.005920   36.444595   1
3   313344  28539152    104.003235  45.072937   242.956024  26.754082   2
4   313344  28539152    635.436829  80.038574   108.716065  35.240089   3
5   313344  28539152    351.910156  80.162117   201.371887  32.738373   4
6   861687  28539165    125.313393  39.836521   231.202873  43.087811   1
7   861687  28539165    623.450500  44.040207   151.332825  34.680435   2
8   1254304 28539165    128.893204  45.765110   225.686691  35.547726   1

Dataframe2值

    Unnamed: 0  user_id subject_id  good    x   y   w   h   T0  T1  T2  T3  T4  T5  T6  T7  T8  target_name target_name_length  target_name3
0   0   858580  23224814    1   58.133331   57.466675   181.000000  42.000000   NaN 1801    No, there are still more names to be marked Male    1881    John Abbott NaN NaN NaN John Abbott 11  John Abbott
1   1   858580  23224814    1   293.133331  176.466675  80.000000   34.000000   NaN NaN Yes, I've marked all the names  Female  NaN NaN Edith Joynt Edith Abbot NaN Edith Joynt 11  Edith Joynt
2   2   340348  30629031    1   152.968750  26.000000   224.000000  41.000000   NaN 1852    No, there are still more names to be marked Male    1924    William Sparrow NaN NaN NaN William Sparrow 15  William Sparrow
3   3   340348  30629031    1   497.968750  325.000000  87.000000   29.000000   NaN NaN Yes, I've marked all the names  Female  NaN NaN Minnie  NaN NaN Minnie  6   Minnie
4   4   340348  28613182    1   103.968750  31.000000   162.000000  38.000000   NaN 1819    No, there are still more names to be marked Male    1876    Albert [unclear]Gles[/unclear]  NaN NaN NaN Albert Gles 30  Albert Gles
5   5   340348  28613182    1   107.968750  76.000000   72.000000   25.000000   NaN 1819    Yes, I've marked all the names  Female  1884    NaN Eliza [unclear]Gles[/unclear]   NaN NaN Eliza Gles  29  Eliza Gles
6   6   340348  30628864    1   172.968750  29.000000   192.000000  41.000000   NaN 1840    No, there are still more names to be marked Male    1918    John Slaltery   NaN NaN NaN John Slaltery   13  John Slaltery
7   7   340348  30628864    1   115.968750  214.000000  149.000000  31.000000   NaN NaN No, there are still more names to be marked Male    NaN [unclear]P.[/unclear] Slaltery  NaN NaN NaN P. Slaltery 30  unclear]P. Slaltery
8   8   340348  30628864    1   537.968750  218.000000  64.000000   26.000000   NaN NaN Yes, I've marked all the names  Female  1901    NaN Elizabeth Slaltery  NaN NaN Elizabeth Slaltery  18  Elizabeth Slaltery

这是我们尝试使用的代码:

python pandas dataframe merge
1个回答
0
投票

如果您想将target列盲目添加到dataframe1,则>>

dataframe1['target'] = dataframe2['target']

只需确保两个数据框具有相同数量的行,并且它们按任何给定的公共列排序。例如:在两个数据框中都找到user_id

© www.soinside.com 2019 - 2024. All rights reserved.