41703
以下のような二つのデータフレームdf1,df2があり、df2はdf1の要素を内容である程度整理したものです。df1にはあってdf2には含まれていない要素で新たな列を作り付け加えたいです。

```

    import pandas as pd
    df1 = pd.DataFrame([["Jack","Male","USA",20,"mucsician","rock","movie"],
                   ["Mary","Female","Canada","game",24,"unemployed","None"],
                   ["Marco",25,"Italy","designer","fashion","tennis","None"],
                   ["Isabel","Female","France",24,"shopping","actress","None"]])
    
```

df1の出力結果

```

          0       1       2       3         4          5           6
    0   Jack    Male     USA     20      musician     rock       movie
    1   Mary    Female  Canada   game      24       unemployed    None
    2   Marco    25     Italy   designer fashion     tennis       None
    3   Isabel  Female  France   24      shopping    actress      None
    
    df2 = pd.DataFrame([["Jack","Male","USA",20,"mucsician"],
                   ["Mary","Female","Canada",24,"unemployed"],
                   ["Marco","None","Italy",25,"designer"],
                   ["Isabel","Female","France",24,"actress"]])
    
```

df2の出力結果

```

          0      1       2      3       4
    0   Jack    Male    USA     20  mucsician
    1   Mary    Female  Canada  24  unemployed
    2   Marco   None    Italy   25  designer
    3   Isabel  Female  France  24  actress
    
```

理想は以下のように列5, 6でdf2にはなかった要素を付け加える形です

```

          0       1       2      3      4        5       6
    0   Jack    Male    USA     20  musician    rock    movie
    1   Mary    Female  Canada  24  unemployed  game    None
    2   Marco   None    Italy   25  designer    fashion tennis
    3   Isabel  Female  France  24  actress    shopping  None
    
```

一列ずつ比べるなら、df2に含まれている要素を列ごとにリスト(list)にして、df1にはカラム名を作り、df1[df1['col_name'].isin(value_list)]のような感じかなと思ったのですが、これだと列がたくさんある場合時間がかかりすぎてしまいます。なにか上手い方法はないでしょうか？
CC BY-SA 3.0
2018-02-14T02:22:54.730
41700
2018-02-14T05:48:48.580
27294
question
python pandas
二つのデータフレームの比較
こんな感じでどうでしょうか？

```

    import pandas as pd
    
    df1 = pd.DataFrame([["Jack","Male","USA",20,"mucsician","rock","movie"],
                   ["Mary","Female","Canada","game",24,"unemployed","None"],
                   ["Marco",25,"Italy","designer","fashion","tennis","None"],
                   ["Isabel","Female","France",24,"shopping","actress","None"]])
    
    df2 = pd.DataFrame([["Jack","Male","USA",20,"mucsician"],
                   ["Mary","Female","Canada",24,"unemployed"],
                   ["Marco","None","Italy",25,"designer"],
                   ["Isabel","Female","France",24,"actress"]])
    
    ret = pd.concat([df2, df1], axis=1).apply(lambda d: d.drop_duplicates().reset_index(drop=True), axis=1)
    print(ret)
    #         0       1       2   3           4         5       6
    # 0    Jack    Male     USA  20   mucsician      rock   movie
    # 1    Mary  Female  Canada  24  unemployed      game    None
    # 2   Marco    None   Italy  25    designer   fashion  tennis
    # 3  Isabel  Female  France  24     actress  shopping    None
    
```
CC BY-SA 3.0
2018-02-14T05:48:48.580
41703
2018-02-14T05:48:48.580
24801
41700
answer