将两个长度不等的dfs与索引的匹配子集合并

2024-10-03 13:31:14 发布

您现在位置:Python中文网/ 问答频道 /正文

说明: 我有两个数据帧foreign(有英语和乌尔都语翻译的选项)和native,有German version of options两个DFs的长度不相等这个nativeforeign大得多,包含完整的选项列表。我想使用idx合并它们两个,以便nativedf在foreign中搜索其idx中的每个条目;如果idx匹配,则其将添加两个columns,即:option_engoption_urdu,否则将其填充为NAN

I have two dataframs
foreign:
idx                    option_eng                    option_urdu
1.1.01-001   This is english of option1             urdu translation of option1
1.1.01-001   This is english of option2             urdu translation of option2
1.1.01-001   This is english of option3             urdu translation of option3 
1.1.01-002   This is english of option1             urdu translation of option1
1.1.01-002   This is english of option2             urdu translation of option2
1.1.01-002   This is english of option3              Urdu translation of option3
1.1.01-003   This is english of option1              urdu translation of option1
1.1.01-003   This is english of option2              urdu translation of option2
1.1.01-004   This is english of option1              urdu translation of option1
1.1.01-004   This is english of option2               urdu translation of option2

Native:>> a unequal to df1
  idx    sort_code  iscorrect   count                option_de
1.1.01-001  1          1          1              German tranlation option3
1.1.01-001  2          1          2              German tranlation option2
1.1.01-001  3          0          3              German tranlation option1
1.1.01-002  1          1          4              German tranlation option3
1.1.01-002  2          1          5             German tranlation option2
1.1.01-002  3          1          6             German tranlation option1
1.1.01-003  1          1          7             German tranlation option3
1.1.01-003  2          1          8             German tranlation option2
1.1.01-003  3          0          9             German tranlation option1
1.1.01-012  1          1          10             German tranlation option3
1.1.01-012  2          1          11             German tranlation option2
1.1.01-012  3          1          12             German tranlation option1
1.1.01-101  1          1          13             German tranlation option3
1.1.01-101  2          1          14             German tranlation option2

我想要的输出应该是

idx               option_de                       option_eng           option_urdu
1.1.01-001  German tranlation option3   This is english of option3  This is urdu of option3
1.1.01-001  German tranlation option2   This is english of option2  This is urdu of option2
1.1.01-001  German tranlation option1   This is english of option1  This is urdu of option1
1.1.01-002  German tranlation option3   This is english of option3  This is urdu of option3
1.1.01-002  German tranlation option2   This is english of option2  This is urdu of option2
1.1.01-002  German tranlation option1   This is english of option1  This is urdu of option1
1.1.01-003  German tranlation option3   This is english of option3  This is urdu of option3
1.1.01-003  German tranlation option2   This is english of option2  This is urdu of option2
1.1.01-003  German tranlation option1   This is english of option1  This is urdu of option1
1.1.01-012  German tranlation option3     na                                  na
1.1.01-012  German tranlation option2     na                                  na
1.1.01-012  German tranlation option1     na                                  na
1.1.01-101  German tranlation option3     na                                  na
1.1.01-101  German tranlation option2     na                                  na


注意 请注意,df2德语选项的排序与选项3、2、1相反,在df1中,选项_en和选项_urdu的顺序相反,我想匹配它们

我试过什么?

我尝试了不同的选择

Merged始终为我提供两个df的重复idx行(类似于每个idx 3次) 我也尝试过pd.combine_first,但它改变了选项的顺序

我用native['key']=native.groupby(["code"]).cumcount()试过了

复制它的源代码

Foreigndic={'idx': {0: '1.1.01-001', 1: '1.1.01-001', 2: '1.1.01-001', 3: '1.1.01-002', 4: '1.1.01-002', 5: '1.1.01-002', 6: '1.1.01-003', 7: '1.1.01-003', 8: '1.1.01-004', 9: '1.1.01-004'}, 'option_eng': {0: ' Stopping as a precaution at every crossroads', 1: ' Not insisting on your rights', 2: " Allowing for other people's mistakes", 3: ' Inattentiveness', 4: ' Driving too close behind the vehicle in front', 5: ' Unexpectedly heavy braking', 6: '  respond as soon as possible to probable changes in the road traffic situation', 7: '  attempt to identify the intentions of other road users, as soon as possible', 8: ' - of its braking characteristics', 9: ' - of its manoeuvrability'}, 'option_urdu': {0: 'احتیاط کے طور پر ہر دوراہے پر رکنا', 1: 'اپنے حقوق پر اصرار نہیں کرنا', 2: 'دوسرے لوگوں کی غلطیوں کی اجازت دینا', 3: 'غفلت', 4: 'اگلی گاڑی سے مناسب فاصلہ نہ رکھنا', 5: 'غیر متوقع طور پر اگلی گاڑی کا اچانک بریک لگا دینا', 6: 'سڑک پر موجود ٹریفک کی صورتحال میں ممکنہ تبدیلیوں کا جلد سے جلد جواب دینا', 7: 'جتنی جلدی ممکن ہو سڑک کے دوسرے صارفین کے ارادوں کی نشاندہی کرنے کی کوشش کریں', 8: ' اس کی بریک خصوصیات', 9: 'اس کے سسٹم کی جانکاری'}}
Nativedict={'code': {0: 1, 1: 2, 2: 3, 3: 1, 4: 2, 5: 3, 6: 1, 7: 2, 8: 3, 9: 1, 10: 2, 11: 3, 12: 1, 13: 2, 14: 3}, 'ans_id': {0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 7, 7: 8, 8: 9, 9: 10, 10: 11, 11: 12, 12: 13, 13: 14, 14: 15}, 'iscorrect': {0: 1, 1: 1, 2: 0, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 0, 9: 1, 10: 1, 11: 1, 12: 1, 13: 1, 14: 1}, 'ans_id_txt': {0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 7, 7: 8, 8: 9, 9: 10, 10: 11, 11: 12, 12: 13, 13: 14, 14: 15}, 'text': {0: 'Nicht auf dem eigenen Recht bestehen', 1: 'Mit Fehlern anderer rechnen', 2: 'Vorsorglich an jeder Kreuzung anhalten', 3: 'Unerwartet starkes Bremsen', 4: 'Unaufmerksamkeit', 5: 'Zu dichtes Auffahren', 6: 'Ich reagiere möglichst frühzeitig auf wahrscheinliche Veränderungen der Verkehrssituation', 7: 'Ich versuche, die Absichten anderer Verkehrsteilnehmer möglichst frühzeitig zu erkennen', 8: 'Ich beschränke die Verkehrsbeobachtung möglichst auf das direkt vor mir fahrende Fahrzeug', 9: 'Ich weiß möglicherweise zu wenig über die Besonderheiten in der Anordnung und der Funktion der Bedienelemente', 10: 'Ich weiß möglicherweise zu wenig über die Besonderheiten im Bremsverhalten', 11: 'Ich weiß möglicherweise zu wenig über die Besonderheiten im Lenkverhalten', 12: 'Der Vorausfahrende bremst unerwartet', 13: 'Der Vorausfahrende betätigt den Blinker vor dem Abbiegen zu spät', 14: 'Der Vorausfahrende hält unerwartet an, um nach dem Weg zu fragen'}, 'ques_id': {0: 1, 1: 1, 2: 1, 3: 2, 4: 2, 5: 2, 6: 3, 7: 3, 8: 3, 9: 4, 10: 4, 11: 4, 12: 5, 13: 5, 14: 5}, 'idx': {0: '1.1.01-001', 1: '1.1.01-001', 2: '1.1.01-001', 3: '1.1.01-002', 4: '1.1.01-002', 5: '1.1.01-002', 6: '1.1.01-003', 7: '1.1.01-003', 8: '1.1.01-003', 9: '1.1.01-004', 10: '1.1.01-004', 11: '1.1.01-004', 12: '1.1.01-101', 13: '1.1.01-101', 14: '1.1.01-101'}}


Tags: ofenglishis选项thistranslationoptiongerman
2条回答

尝试通过添加order元素为每个idx组创建唯一索引。然后在idx和order列上合并

import pandas as pd

foreign = pd.DataFrame({'idx': {0: '1.1.01-001', 1: '1.1.01-001',
                                2: '1.1.01-001', 3: '1.1.01-002',
                                4: '1.1.01-002', 5: '1.1.01-002',
                                6: '1.1.01-003', 7: '1.1.01-003',
                                8: '1.1.01-004', 9: '1.1.01-004'},
                        'option_eng': {
                            0: ' Stopping as a precaution at every crossroads',
                            1: ' Not insisting on your rights',
                            2: " Allowing for other people's mistakes",
                            3: ' Inattentiveness',
                            4: ' Driving too close behind the vehicle in front',
                            5: ' Unexpectedly heavy braking',
                            6: '  respond as soon as possible to probable changes in the road traffic situation',
                            7: '  attempt to identify the intentions of other road users, as soon as possible',
                            8: ' - of its braking characteristics',
                            9: ' - of its manoeuvrability'
                        },
                        'option_urdu': {
                            0: 'احتیاط کے طور پر ہر دوراہے پر رکنا',
                            1: 'اپنے حقوق پر اصرار نہیں کرنا',
                            2: 'دوسرے لوگوں کی غلطیوں کی اجازت دینا',
                            3: 'غفلت',
                            4: 'اگلی گاڑی سے مناسب فاصلہ نہ رکھنا',
                            5: 'غیر متوقع طور پر اگلی گاڑی کا اچانک بریک لگا دینا',
                            6: 'سڑک پر موجود ٹریفک کی صورتحال میں ممکنہ تبدیلیوں کا جلد سے جلد جواب دینا',
                            7: 'جتنی جلدی ممکن ہو سڑک کے دوسرے صارفین کے ارادوں کی نشاندہی کرنے کی کوشش کریں',
                            8: ' اس کی بریک خصوصیات',
                            9: 'اس کے سسٹم کی جانکاری'}
                        })

native = pd.DataFrame(
    {'code': {0: 1, 1: 2, 2: 3, 3: 1, 4: 2, 5: 3, 6: 1, 7: 2,
              8: 3, 9: 1, 10: 2, 11: 3, 12: 1, 13: 2, 14: 3},
     'ans_id': {0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 7, 7: 8,
                8: 9, 9: 10, 10: 11, 11: 12, 12: 13, 13: 14, 14: 15},
     'iscorrect': {0: 1, 1: 1, 2: 0, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1,
                   8: 0, 9: 1, 10: 1, 11: 1, 12: 1, 13: 1, 14: 1},
     'ans_id_txt': {0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 7, 7: 8,
                    8: 9, 9: 10, 10: 11, 11: 12, 12: 13, 13: 14,
                    14: 15},
     'text': {0: 'Nicht auf dem eigenen Recht bestehen',
              1: 'Mit Fehlern anderer rechnen',
              2: 'Vorsorglich an jeder Kreuzung anhalten',
              3: 'Unerwartet starkes Bremsen',
              4: 'Unaufmerksamkeit',
              5: 'Zu dichtes Auffahren',
              6: 'Ich reagiere möglichst frühzeitig auf '
                 'wahrscheinliche Veränderungen der Verkehrssituation',
              7: 'Ich versuche, die Absichten anderer '
                 'Verkehrsteilnehmer möglichst frühzeitig zu erkennen',
              8: 'Ich beschränke die Verkehrsbeobachtung '
                 'möglichst auf das direkt vor mir fahrende Fahrzeug',
              9: 'Ich weiß möglicherweise zu wenig über die Besonderheiten '
                 'in der Anordnung und der Funktion der Bedienelemente',
              10: 'Ich weiß möglicherweise zu wenig über '
                  'die Besonderheiten im Bremsverhalten',
              11: 'Ich weiß möglicherweise zu wenig über '
                  'die Besonderheiten im Lenkverhalten',
              12: 'Der Vorausfahrende bremst unerwartet',
              13: 'Der Vorausfahrende betätigt den Blinker vor dem Abbiegen zu spät',
              14: 'Der Vorausfahrende hält unerwartet an, um nach dem Weg zu fragen'},
     'ques_id': {0: 1, 1: 1, 2: 1, 3: 2, 4: 2, 5: 2, 6: 3, 7: 3,
                 8: 3, 9: 4, 10: 4, 11: 4, 12: 5, 13: 5, 14: 5},
     'idx': {0: '1.1.01-001', 1: '1.1.01-001', 2: '1.1.01-001',
             3: '1.1.01-002', 4: '1.1.01-002', 5: '1.1.01-002',
             6: '1.1.01-003', 7: '1.1.01-003', 8: '1.1.01-003',
             9: '1.1.01-004', 10: '1.1.01-004', 11: '1.1.01-004',
             12: '1.1.01-101', 13: '1.1.01-101', 14: '1.1.01-101'}}
)

# Grab Columns and add key for group order (level_1)
foreign = foreign[['idx', 'option_eng', 'option_urdu']].groupby('idx') \
    .apply(lambda x: x.reset_index(drop=True)) \
    .drop(columns=['idx']) \
    .reset_index()
native = native[['idx', 'text']].groupby('idx') \
    .apply(lambda x: x.reset_index(drop=True)) \
    .drop(columns=['idx']) \
    .reset_index()

# Merge Together on idx and level_1
merged = native.merge(foreign, on=['idx', 'level_1'], how='outer') \
    .drop(columns='level_1') \
    .fillna('na')

print(merged.to_string(index=False))
^{tb1}$

旁白:您提供的数据似乎没有正确对齐。Foreign:1.1.01-002,0,Inattentiveness是1.1.01-002组的第一行,但它与Native:1.1.01-002,0,Unerwartet starkes Bremsen对齐,这是不正确的。它应该与第二行1.1.01-002,1,Unaufmerksamkeit对齐。但是假设在两个表中都有正确顺序的翻译,这应该是可行的

假设不需要对表进行排序,pd.concat()使用axis=1作为参数就可以了

pd.concat([
    native[['idx', 'option_de']], 
    foreign[['option_eng', 'option_urdu']]
], axis=1)

这对我来说很有效

native['key']=native.groupby(["code"]).cumcount()
native= native.sort_values(['key','code'],ascending=[True,False]).drop('key',1).reset_index()

native=native.rename(columns={'idx.1':'idx'})
mrg=pd.concat([native[['idx', 'text','iscorrect']],foreign[['option_eng', 'option_urdu']]], axis=1)

相关问题 更多 >