我想合并两个共享一些相同变量的数据集,以使一个数据集具有变量' EF'和' LF'适当分配。但是,对于其中一个变量('项目'),两个数据集的复杂程度不同。我在下面提供了两个数据集(它们本身都是较大数据集的子集)。
似乎合并正确发生,但输出并不是我所期望的。具体而言(例如),肉类'结果为' Item.x'的级别名称与“Item.y'”不匹配。我只保留其中一个' Item'合并完成后的列,但我想检查' Item.y'的输出。在忽略(和删除)' Item.x'。
之前,这个概念是正确的从以前的协助和搜索中,我尝试了以下代码的各种版本来合并df1和df2:
Region Group Item Stage LF
Europe Fruit.Veg FSC1 20
Europe Fruit.Veg FSC2 5
Europe Fruit.Veg FSC3 2
Europe Fruit.Veg FSC4f 5.8935
Europe Fruit.Veg FSC4p 2
Europe Fruit.Veg FSC5f 19
Europe Fruit.Veg FSC5p 15
Europe Meat Bovine FSC1 2.3
Europe Meat Mutton FSC1 10
Europe Meat Goat FSC1 10
Europe Meat Pig FSC1 2.5
Europe Meat Poultry FSC1 7
Europe Meat Bovine FSC2 0.613
Europe Meat Mutton FSC2 0.618
Europe Meat Goat FSC2 0.618
Europe Meat Pig FSC2 0.23
Europe Meat Poultry FSC2 1.765
Europe Meat FSC3 5
Europe Meat FSC4 4.033333333
Europe Meat FSC5 11
Europe Marine FSC1 9.4
Europe Marine FSC2 0.5
Europe Marine FSC3 6
Europe Marine FSC4f 9
Europe Marine FSC4p 5
Europe Marine FSC5f 11
Europe Marine FSC5p 10
Europe Milk.Eggs Milk FSC1 3.5
Europe Milk.Eggs Milk FSC2 0.5
Europe Milk.Eggs Milk FSC3 1.2
Europe Milk.Eggs Milk FSC4 0.713333333
Europe Milk.Eggs Milk FSC5 7
Europe Milk.Eggs Eggs FSC1 4
Europe Milk.Eggs Eggs FSC2 0
Europe Milk.Eggs Eggs FSC3 0.5
Europe Milk.Eggs Eggs FSC4 2
Europe Milk.Eggs Eggs FSC5 8
例如,在命令中更改df1和df2的顺序,或添加" Item"进入' by'函数提供的输出完全不正确。
我错过了什么?
谢谢!
INPUT DF1
Region Sub-Region Group Item EF
Europe Europe Fruit.Veg Apples 0.227686081
Europe Europe Fruit.Veg Bananas 0.35
Europe Europe Fruit.Veg Citrus 0.344375
Europe Europe Fruit.Veg Grapes 0.41
Europe Europe Fruit.Veg Fruit.Other 2.029212613
Europe Europe Fruit.Veg Vegetables 0.917924249
Europe Europe Meat Bovine 22.68119086
Europe Europe Meat Mutton 20.99167461
Europe Europe Meat Goat 18.19083333
Europe Europe Meat Pig 5.04123312
Europe Europe Meat Poultry 3.712
Europe Europe Marine Fish.Seafood 4.117510463
Europe Europe Milk.Eggs Milk 1.308859756
Europe Europe Milk.Eggs Eggs 3.83
DF2
Region Group Item.x Stage LF Sub.Region Item.y EF
Europe Fruit.Veg FSC1 20 Europe Apples 0.227686081
Europe Fruit.Veg FSC1 20 Europe Bananas 0.35
Europe Fruit.Veg FSC1 20 Europe Citrus 0.344375
Europe Fruit.Veg FSC1 20 Europe Grapes 0.41
Europe Fruit.Veg FSC1 20 Europe Fruit.Other 2.029212613
Europe Fruit.Veg FSC1 20 Europe Vegetables 0.917924249
Europe Fruit.Veg FSC2 5 Europe Apples 0.227686081
Europe Fruit.Veg FSC2 5 Europe Bananas 0.35
Europe Fruit.Veg FSC2 5 Europe Citrus 0.344375
Europe Fruit.Veg FSC2 5 Europe Grapes 0.41
Europe Fruit.Veg FSC2 5 Europe Fruit.Other 2.029212613
Europe Fruit.Veg FSC2 5 Europe Vegetables 0.917924249
Europe Fruit.Veg FSC3 2 Europe Apples 0.227686081
Europe Fruit.Veg FSC3 2 Europe Bananas 0.35
Europe Fruit.Veg FSC3 2 Europe Citrus 0.344375
Europe Fruit.Veg FSC3 2 Europe Grapes 0.41
Europe Fruit.Veg FSC3 2 Europe Fruit.Other 2.029212613
Europe Fruit.Veg FSC3 2 Europe Vegetables 0.917924249
Europe Fruit.Veg FSC4f 5.8935 Europe Apples 0.227686081
Europe Fruit.Veg FSC4f 5.8935 Europe Bananas 0.35
Europe Fruit.Veg FSC4f 5.8935 Europe Citrus 0.344375
Europe Fruit.Veg FSC4f 5.8935 Europe Grapes 0.41
Europe Fruit.Veg FSC4f 5.8935 Europe Fruit.Other 2.029212613
Europe Fruit.Veg FSC4f 5.8935 Europe Vegetables 0.917924249
Europe Fruit.Veg FSC4p 2 Europe Apples 0.227686081
Europe Fruit.Veg FSC4p 2 Europe Bananas 0.35
Europe Fruit.Veg FSC4p 2 Europe Citrus 0.344375
Europe Fruit.Veg FSC4p 2 Europe Grapes 0.41
Europe Fruit.Veg FSC4p 2 Europe Fruit.Other 2.029212613
Europe Fruit.Veg FSC4p 2 Europe Vegetables 0.917924249
Europe Fruit.Veg FSC5f 19 Europe Apples 0.227686081
Europe Fruit.Veg FSC5f 19 Europe Bananas 0.35
Europe Fruit.Veg FSC5f 19 Europe Citrus 0.344375
Europe Fruit.Veg FSC5f 19 Europe Grapes 0.41
Europe Fruit.Veg FSC5f 19 Europe Fruit.Other 2.029212613
Europe Fruit.Veg FSC5f 19 Europe Vegetables 0.917924249
Europe Fruit.Veg FSC5p 15 Europe Apples 0.227686081
Europe Fruit.Veg FSC5p 15 Europe Bananas 0.35
Europe Fruit.Veg FSC5p 15 Europe Citrus 0.344375
Europe Fruit.Veg FSC5p 15 Europe Grapes 0.41
Europe Fruit.Veg FSC5p 15 Europe Fruit.Other 2.029212613
Europe Fruit.Veg FSC5p 15 Europe Vegetables 0.917924249
Europe Marine FSC1 9.4 Europe Fish.Seafood 4.117510463
Europe Marine FSC2 0.5 Europe Fish.Seafood 4.117510463
Europe Marine FSC3 6 Europe Fish.Seafood 4.117510463
Europe Marine FSC4f 9 Europe Fish.Seafood 4.117510463
Europe Marine FSC4p 5 Europe Fish.Seafood 4.117510463
Europe Marine FSC5f 11 Europe Fish.Seafood 4.117510463
Europe Marine FSC5p 10 Europe Fish.Seafood 4.117510463
Europe Meat Bovine FSC1 2.3 Europe Bovine 22.68119086
Europe Meat Bovine FSC1 2.3 Europe Mutton 20.99167461
Europe Meat Bovine FSC1 2.3 Europe Goat 18.19083333
Europe Meat Bovine FSC1 2.3 Europe Pig 5.04123312
Europe Meat Bovine FSC1 2.3 Europe Poultry 3.712
Europe Meat Mutton FSC1 10 Europe Bovine 22.68119086
Europe Meat Mutton FSC1 10 Europe Mutton 20.99167461
Europe Meat Mutton FSC1 10 Europe Goat 18.19083333
Europe Meat Mutton FSC1 10 Europe Pig 5.04123312
Europe Meat Mutton FSC1 10 Europe Poultry 3.712
Europe Meat Goat FSC1 10 Europe Bovine 22.68119086
Europe Meat Goat FSC1 10 Europe Mutton 20.99167461
Europe Meat Goat FSC1 10 Europe Goat 18.19083333
Europe Meat Goat FSC1 10 Europe Pig 5.04123312
Europe Meat Goat FSC1 10 Europe Poultry 3.712
Europe Meat Pig FSC1 2.5 Europe Bovine 22.68119086
Europe Meat Pig FSC1 2.5 Europe Mutton 20.99167461
Europe Meat Pig FSC1 2.5 Europe Goat 18.19083333
Europe Meat Pig FSC1 2.5 Europe Pig 5.04123312
Europe Meat Pig FSC1 2.5 Europe Poultry 3.712
Europe Meat Poultry FSC1 7 Europe Bovine 22.68119086
Europe Meat Poultry FSC1 7 Europe Mutton 20.99167461
Europe Meat Poultry FSC1 7 Europe Goat 18.19083333
Europe Meat Poultry FSC1 7 Europe Pig 5.04123312
Europe Meat Poultry FSC1 7 Europe Poultry 3.712
Europe Meat Bovine FSC2 0.613 Europe Bovine 22.68119086
Europe Meat Bovine FSC2 0.613 Europe Mutton 20.99167461
Europe Meat Bovine FSC2 0.613 Europe Goat 18.19083333
Europe Meat Bovine FSC2 0.613 Europe Pig 5.04123312
Europe Meat Bovine FSC2 0.613 Europe Poultry 3.712
Europe Meat Mutton FSC2 0.618 Europe Bovine 22.68119086
Europe Meat Mutton FSC2 0.618 Europe Mutton 20.99167461
Europe Meat Mutton FSC2 0.618 Europe Goat 18.19083333
Europe Meat Mutton FSC2 0.618 Europe Pig 5.04123312
Europe Meat Mutton FSC2 0.618 Europe Poultry 3.712
Europe Meat Goat FSC2 0.618 Europe Bovine 22.68119086
Europe Meat Goat FSC2 0.618 Europe Mutton 20.99167461
Europe Meat Goat FSC2 0.618 Europe Goat 18.19083333
Europe Meat Goat FSC2 0.618 Europe Pig 5.04123312
Europe Meat Goat FSC2 0.618 Europe Poultry 3.712
Europe Meat Pig FSC2 0.23 Europe Bovine 22.68119086
Europe Meat Pig FSC2 0.23 Europe Mutton 20.99167461
Europe Meat Pig FSC2 0.23 Europe Goat 18.19083333
Europe Meat Pig FSC2 0.23 Europe Pig 5.04123312
Europe Meat Pig FSC2 0.23 Europe Poultry 3.712
Europe Meat Poultry FSC2 1.765 Europe Bovine 22.68119086
Europe Meat Poultry FSC2 1.765 Europe Mutton 20.99167461
Europe Meat Poultry FSC2 1.765 Europe Goat 18.19083333
Europe Meat Poultry FSC2 1.765 Europe Pig 5.04123312
Europe Meat Poultry FSC2 1.765 Europe Poultry 3.712
Europe Meat FSC3 5 Europe Bovine 22.68119086
Europe Meat FSC3 5 Europe Mutton 20.99167461
Europe Meat FSC3 5 Europe Goat 18.19083333
Europe Meat FSC3 5 Europe Pig 5.04123312
Europe Meat FSC3 5 Europe Poultry 3.712
Europe Meat FSC4 4.033333333 Europe Bovine 22.68119086
Europe Meat FSC4 4.033333333 Europe Mutton 20.99167461
Europe Meat FSC4 4.033333333 Europe Goat 18.19083333
Europe Meat FSC4 4.033333333 Europe Pig 5.04123312
Europe Meat FSC4 4.033333333 Europe Poultry 3.712
Europe Meat FSC5 11 Europe Bovine 22.68119086
Europe Meat FSC5 11 Europe Mutton 20.99167461
Europe Meat FSC5 11 Europe Goat 18.19083333
Europe Meat FSC5 11 Europe Pig 5.04123312
Europe Meat FSC5 11 Europe Poultry 3.712
Europe Milk.Eggs Milk FSC1 3.5 Europe Milk 1.308859756
Europe Milk.Eggs Milk FSC1 3.5 Europe Eggs 3.83
Europe Milk.Eggs Milk FSC2 0.5 Europe Milk 1.308859756
Europe Milk.Eggs Milk FSC2 0.5 Europe Eggs 3.83
Europe Milk.Eggs Milk FSC3 1.2 Europe Milk 1.308859756
Europe Milk.Eggs Milk FSC3 1.2 Europe Eggs 3.83
Europe Milk.Eggs Milk FSC4 0.713333333 Europe Milk 1.308859756
Europe Milk.Eggs Milk FSC4 0.713333333 Europe Eggs 3.83
Europe Milk.Eggs Milk FSC5 7 Europe Milk 1.308859756
Europe Milk.Eggs Milk FSC5 7 Europe Eggs 3.83
Europe Milk.Eggs Eggs FSC1 4 Europe Milk 1.308859756
Europe Milk.Eggs Eggs FSC1 4 Europe Eggs 3.83
Europe Milk.Eggs Eggs FSC2 0 Europe Milk 1.308859756
Europe Milk.Eggs Eggs FSC2 0 Europe Eggs 3.83
Europe Milk.Eggs Eggs FSC3 0.5 Europe Milk 1.308859756
Europe Milk.Eggs Eggs FSC3 0.5 Europe Eggs 3.83
Europe Milk.Eggs Eggs FSC4 2 Europe Milk 1.308859756
Europe Milk.Eggs Eggs FSC4 2 Europe Eggs 3.83
Europe Milk.Eggs Eggs FSC5 8 Europe Milk 1.308859756
Europe Milk.Eggs Eggs FSC5 8 Europe Eggs 3.83
OUTPUT merge.df
#container-1{
display: table;
position: relative;
top: 50px;
width: 1000px;
margin: 0px auto;
background: rgba(255, 255, 255, 0.5);
}
答案 0 :(得分:0)
当您合并区域和群组时,您正在进行多对多合并。因此,例如,对于Europe和Fruit.Veg,您在第一个df中有7个记录,在第二个df中有6个记录,因此您在合并表中最终得到42(7 * 6)个记录 - 这对于许多人来说是正确的行为到多少合并。
看起来你想要的是在合并的by语句中包含Item,但它可能没有给你你期望的行为,因为你的某些df1记录中缺少Item。如果不是这种情况,您能否解释在将Item添加到合并语句时不正确的行为。