1

我拖曳或三个具有重复行的 DataFrame。

In [31]: df1
Out[31]: 
    member           time
0       0 2009-09-30 12:00:00
1       0 2009-09-30 18:00:00
2       0 2009-10-01 00:00:00
3       1 2009-09-30 12:00:00
4       1 2009-09-30 18:00:00
5       2 2009-09-30 12:00:00
6       3 2009-09-30 12:00:00
...

In [32]: df2
Out[32]: 
    member           time
0       0 2009-09-30 12:00:00
1       0 2009-09-30 18:00:00
3       1 2009-09-30 12:00:00
4       2 2009-09-30 12:00:00
5       2 2009-09-30 18:00:00
6       2 2009-10-01 00:00:00
...

我想从 df1 和 df2 中过滤掉具有唯一值 'member' 和 'time' 的行,并得到一个 DataFrame,其中只有在 df1 和df2,即

In [33]: df_duplicated_1_and_2
Out[33]: 
    member           time
0       0 2009-09-30 12:00:00
1       0 2009-09-30 18:00:00
3       1 2009-09-30 12:00:00
4       2 2009-09-30 12:00:00
...

有没有一种有效而优雅的方法来做到这一点?

更新如果可能的话,我不想得到一个新的合并 DataFrame,而是一个过滤的 DataFrame。例如,

In [34]: df1
Out[34]: 
    member           time           value
0       0 2009-09-30 12:00:00  a
1       0 2009-09-30 18:00:00  b
2       0 2009-10-01 00:00:00  c
3       1 2009-09-30 12:00:00  d
4       1 2009-09-30 18:00:00  e
5       2 2009-09-30 12:00:00  f
6       3 2009-09-30 12:00:00  g
...

In [35]: df1_filtered_out
Out[35]: 
    member           time           value
0       0 2009-09-30 12:00:00  a
1       0 2009-09-30 18:00:00  b
3       1 2009-09-30 12:00:00  d
4       2 2009-09-30 12:00:00  g
...

并获得过滤的df2。

4

1 回答 1

3

membertime列进行内部连接:

>>> df1.merge(df2, on=['member', 'time'], how='inner')
   member                time
0       0 2009-09-30 12:00:00
1       0 2009-09-30 18:00:00
2       1 2009-09-30 12:00:00
3       2 2009-09-30 12:00:00

这将产生一个结果,其中只有两个 DataFrame中具有相同member和值的行。time

更新:

>>> df1.merge(df2[['member', 'time']])
   member                time value
0       0 2009-09-30 12:00:00     a
1       0 2009-09-30 18:00:00     b
2       1 2009-09-30 12:00:00     d
3       2 2009-09-30 12:00:00     f
于 2013-09-23T08:41:01.580 回答