Table1
Oid, T1
10, 1493955900309445045
10, 1493955900321006000
10, 1493955900322255462
11, 14910000
表2:
Oid,T2
10, 1493955900309206537
10, 1493955900320695981
11, 1490000
Expected merge output
Oid,T1,T2
10, 1493955900309445045,1493955900309206537
10, 1493955900321006000,1493955900320695981
#ignored 10, 1493955900322255462 #mapped nowhere
11, 14910000,1490000
基本上合并匹配的第一个实例,忽略剩余。我知道数据是按顺序排列的(有些值缺少表2),所以我需要忽略这些情况。为了简单起见,我们可以假设表1是某个任务的开始,而表2包含一些任务的结束。总有一天任务可能会结束,但不会结束!我认为这可以通过索引来完成
补充:
假设我们只想在两个Oid中的条目数相同的情况下合并。预期合并输出变成
Oid,T1,T2
#ignored all Oid = 10,because there count in table 1=3,table2=3
11, 14910000,1490000
另一个例子
>>> df1
Oid ts
0 10 1000
1 10 1001
2 20 2000
3 20 2001
4 30 3000
5 40 4000
>>> df2
Oid ts2
0 10 1006
1 10 1007
2 10 1008
3 20 2005
4 20 2004
5 30 3003
6 40 4004
7 40 4008
Expected Output
Oid ts ts2
20 2000 2005
20 2001 2004
30 3000 3003
我认为使用value_counts会有帮助,我做到了
>>> df1.Oid.value_counts()
20 2
10 2
30 1
40 1
Name: Oid, dtype: int64
>>> df2.Oid.value_counts()
10 3
20 2
40 2
30 1
现在只取20和30,因为只有那才算比赛。我认为我需要创建好的订单列表,这些订单将从df1和df2中过滤,然后合并。
@jezrael您的答案似乎适用于整个列,而不是Oid列中的每个唯一条目
发布于 2017-05-16 12:48:38
您可以使用cumcount
按Oid
列计数重复项,然后使用merge
。最后一次通过new
删除助手列drop
df1['new'] = df1.groupby('Oid').cumcount()
df2['new'] = df2.groupby('Oid').cumcount()
print (df1)
Oid T1 new
0 10 1493955900309445045 0
1 10 1493955900321006000 1
2 10 1493955900322255462 2
3 11 14910000 0
print (df2)
Oid T2 new
0 10 1493955900309206537 0
1 10 1493955900320695981 1
2 11 1490000 0
df = pd.merge(df1, df2, on=['Oid','new']).drop('new', axis=1)
print (df)
Oid T1 T2
0 10 1493955900309445045 1493955900309206537
1 10 1493955900321006000 1493955900320695981
2 11 14910000 1490000
编辑以检查列是否相同使用equals
if df1['Oid'].equals(df2['Oid']):
print ('eq')
#another code
else:
print ('no')
#another code
另一个可能的解决方案是,只有列中的测试值与Series.eq
(与==
相同)和all
相同。
if (df1['Oid'].eq(df2['Oid'])).all():
print ('eq')
#another code
else:
print ('no')
#another code
EDIT1:
首先获得长度相同的oids
:
a = df1.Oid.value_counts()
b = df2.Oid.value_counts()
df1 = df1.set_index('Oid')
df2 = df2.set_index('Oid')
c = pd.concat([a,b], axis=1, keys=('a','b'))
oids = c.index[c['a'] == c['b']]
print (oids)
Int64Index([20, 30], dtype='int64')
然后由oids
和concat
进行选择
df3 = pd.concat([df1.loc[oids],df2.loc[oids]], axis=1).reset_index()
print (df3)
Oid ts ts2
0 20 2000 2005
1 20 2001 2004
2 30 3000 3003
https://stackoverflow.com/questions/44001853
复制相似问题