需要通过比较2个数据帧来查找缺失的列值

时间:2018-02-05 16:10:39

标签: python pandas

DF1:  A   B
      1   a
      2   b
      3   c
      4   d
      5   e

DF2:  A   B
      1   a
      2   b
      3   c

Expect Result:   A  B
                 4  d
                 5  e

尝试加入&地图但没有得到理想的结果。

请指导找到解决方案。

3 个答案:

答案 0 :(得分:2)

concatdrop_duplicates

pd.concat([df1,df2]).drop_duplicates(keep=False)
Out[789]: 
   A  B
3  4  d
4  5  e

答案 1 :(得分:0)

试试这个:

#ifndef ENCODE
#define ENCODE

#include "ImageEffect.h"
#include "time.h"

//this image effect will encode an image
class Encode : public ImageEffect
{
    virtual void processImage(PpmDocument &doc)
    {
        {
            int x = 2, y = 2;
            Pixel& p = doc.getPixel(0, 0);
            srand(time(NULL));

            p.setGreen(x);
            p.setBlue(y);

            string message = "test message";
            p.setRed((int)message[0]);
            for (int i = 1; i < message.size(); i++)
            {
                p.setRed((int)message[i]);
                x += (rand() % 3) + 1;
                y += (rand() % 3) + 1;
                p.setGreen(x);
                p.setBlue(y);
                Pixel& p = doc.getPixel(x, y);
            }
            p.setGreen(0);
            p.setBlue(0);
        }
    }
};

#endif // !ENCODE

输出:

df1[~df1.isin(df2)].dropna()

答案 2 :(得分:0)

这也可行:

df1.loc[~df1['A'].isin(df2['A'])]
Out[34]:
    A   B
3   4   d
4   5   e