简体   繁体   English

使用Pandas在Python中产生有趣的数据帧

[英]Intercest dataframes in python using pandas

I have two DataFrames: A and B. A comtains two columns: Number and Letter, B also contains two columns: Number1 and Number2, and Number contains same data as in both Number1 and Number2. 我有两个数据框:A和B。A包含两列:Number和Letter,B也包含两列:Number1和Number2,并且Number包含与Number1和Number2中相同的数据。 How can I make a ned dataframe which will have column which contains Number1, Number2 and column with its Letter? 如何制作一个包含列的数字框,该列包含Number1,Number2和带有Letter的列?

 A
Number Letter
1        e
2        l
3        o
4        s
5        p
6        w 
7        r
8        i
9        u

B
Number1 Number2
1           7
4           9
2           1

Needed output (without dublicats): 所需的输出(没有引号):

Number Letter
    1     e
    4     s
    2     l
    7     r
    9     u

If there was only one column in B, i would make it with "merge", but how to be here? 如果B中只有一列,那么我将其与“合并”合并,但是该怎么办? Can anybody help? 有人可以帮忙吗?

you can use stack() in conjunction with isin() : 您可以将stack()isin()结合使用:

In [109]: A[A['Number'].isin(B.stack())]
Out[109]:
   Number Letter
0       1      e
1       2      l
3       4      s
6       7      r
8       9      u

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM