2016-12-14 125 views
3

我有两个数据帧,我试图合并。在浮点值列上合并pandas DataFrame

数据框答:

col1 col2 sub grade 
0 1  34.32 x  a 
1 1  34.32 x  b 
2 1  34.33 y  c 
3 2  10.14 z  b 
4 3  33.01 z  a 

数据帧B:

col1 col2 group ID 
0 1  34.32 t  z 
1 1  54.32 s  w 
2 1  34.33 r  z 
3 2  10.14 q  z 
4 3  33.01 q  e 

我想col1和col2上合并。我一直pd.merge用的语法如下:

pd.merge(A, B, how = 'outer', on = ['col1', 'col2']) 

不过,我觉得我遇到了加盟COL2上的浮点值的问题,因为许多行被丢弃。有没有办法使用np.isclose来匹配col2的值?当我在任何数据帧中引用col2的特定值的索引时,该值的数字小数多于数据帧中显示的数字。

我想结果是:

col1 col2 sub grade group ID 
0 1  34.32 x  a  t  z 
1 1  34.32 x  b  s  w 
2 1  54.32 s  w  NaN  NaN 
3 1  34.33 y  c  r  z 
4 2  10.14 z  b  q  z 
5 3  33.01 z  a  q  e 
+0

显示应如何看待预期结果 – RomanPerekhrest

+0

我已编辑帖子以反映所需的输出。 – Megan

+0

@Megan在你的连接中你删除了一些行。检查我的答案如何基于两列进行合并。合并后,您可以应用其他逻辑。 – MYGz

回答

3

您可以使用一个小巧的黑客工具 - 通过一些常量像1001000 ...,将列由多个浮动列intmerge和最后的鸿沟常量:

N = 100 
#thank you koalo for comment 
A.col2 = np.round(A.col2*N).astype(int) 
B.col2 = np.round(B.col2*N).astype(int) 
df = pd.merge(A, B, how = 'outer', on = ['col1', 'col2']) 
df.col2 = df.col2/N 
print (df) 
    col1 col2 sub grade group ID 
0  1 34.32 x  a  t z 
1  1 34.32 x  b  t z 
2  1 34.33 y  c  r z 
3  2 10.14 z  b  q z 
4  3 33.01 z  a  q e 
5  1 54.32 NaN NaN  s w 
+0

哦。加入小数点不准确? – MYGz

+0

是的,这是可能的,但我认为这是低效的看到这[评论](http://stackoverflow.com/questions/38114654/pandas-read-csv-column-dtype-is-set-to-decimal-but转换为字符串/ 38114744#comment63666221_38114744) – jezrael

+0

好的方法,但由于舍入错误导致更细微的错误。更好地使用A.col2 = np.round(A.col2 * N).astype(int) – koalo

相关问题