合并pandas数据帧:在左

时间:2016-03-10 01:37:04

标签: python pandas floating-point

我有几个数据集,我试图合并为一个。下面,我创建了更简单的更简单的小数据集来测试方法,它工作得很好。

examplelog = pd.DataFrame({'Depth':[10,20,30,40,50,60,70,80], 
                       'TVD':[10,19.9,28.8,37.7,46.6,55.5,64.4,73.3],
                       'T1':[11,11.3,11.5,12.,12.3,12.6,13.,13.8],
                       'T2':[11.3,11.5,11.8,12.2,12.4,12.7,13.1,14.1]})

log1 = pd.DataFrame({'Depth':[30,40,50,60],'T3':[12.1,12.6,13.7,14.]})


log2 = pd.DataFrame({'Depth':[20,30,40,50,60],'T4':[12.0,12.2,12.4,13.2,14.1]})

logs=[log1,log2]

result=examplelog.copy()

for i in logs:
    result=result.merge(i,how='left', on='Depth')
print result

结果如预期:

Depth    T1    T2   TVD    T3    T4
0     10  11.0  11.3  10.0   NaN   NaN
1     20  11.3  11.5  19.9   NaN  12.0
2     30  11.5  11.8  28.8  12.1  12.2
3     40  12.0  12.2  37.7  12.3  12.4
4     50  12.3  12.4  46.6  13.5  13.2
5     60  12.6  12.7  55.5  14.2  14.1
6     70  13.0  13.1  64.4   NaN   NaN
7     80  13.8  14.1  73.3   NaN   NaN

对结果感到满意,我将此方法应用于我的实际数据,但对于结果数据帧中的T3和T4,我只收到空列(所有值均为NaN)。我怀疑问题是浮点数,因为我的数据集是由不同的软件在不同的机器上创建的,虽然“深度”在所有文件中都有两个十进制数的精度,但我担心它可能不是20.05他们两个,但一个可能是20.049999999999999而在另一个可能是20.05000000000001。然后,合并功能将不起作用,如以下示例所示:

examplelog = pd.DataFrame({'Depth':[10,20,30,40,50,60,70,80], 
                           'TVD':[10,19.9,28.8,37.7,46.6,55.5,64.4,73.3],
                           'T1':[11,11.3,11.5,12.,12.3,12.6,13.,13.8],
                           'T2':[11.3,11.5,11.8,12.2,12.4,12.7,13.1,14.1]})

log1 = pd.DataFrame({'Depth':[30.05,40.05,50.05,60.05],'T3':[12.1,12.6,13.7,14.]})


log2 = pd.DataFrame({'Depth':[20.01,30.01,40.01,50.01,60.01],'T4':[12.0,12.2,12.4,13.2,14.1]})

logs=[log1,log2]

result=examplelog.copy()

for i in logs:
    result=result.merge(i,how='left', on='Depth')
print result

   Depth    T1    T2   TVD  T3  T4
0     10  11.0  11.3  10.0 NaN NaN
1     20  11.3  11.5  19.9 NaN NaN
2     30  11.5  11.8  28.8 NaN NaN
3     40  12.0  12.2  37.7 NaN NaN
4     50  12.3  12.4  46.6 NaN NaN
5     60  12.6  12.7  55.5 NaN NaN
6     70  13.0  13.1  64.4 NaN NaN
7     80  13.8  14.1  73.3 NaN NaN

你知道如何解决这个问题吗? 谢谢!

1 个答案:

答案 0 :(得分:0)

Depth值四舍五入为适当的精度:

for df in [examplelog, log1, log2]:
    df['Depth'] = df['Depth'].round(1)
import numpy as np
import pandas as pd

examplelog = pd.DataFrame({'Depth':[10,20,30,40,50,60,70,80], 
                           'TVD':[10,19.9,28.8,37.7,46.6,55.5,64.4,73.3],
                           'T1':[11,11.3,11.5,12.,12.3,12.6,13.,13.8],
                           'T2':[11.3,11.5,11.8,12.2,12.4,12.7,13.1,14.1]})

log1 = pd.DataFrame({'Depth':[30.05,40.05,50.05,60.05],'T3':[12.1,12.6,13.7,14.]})
log2 = pd.DataFrame({'Depth':[20.01,30.01,40.01,50.01,60.01],
                     'T4':[12.0,12.2,12.4,13.2,14.1]})

for df in [examplelog, log1, log2]:
    df['Depth'] = df['Depth'].round(1)

logs=[log1,log2]
result=examplelog.copy()
for i in logs:
    result=result.merge(i,how='left', on='Depth')
print(result)

产量

   Depth    T1    T2   TVD    T3    T4
0     10  11.0  11.3  10.0   NaN   NaN
1     20  11.3  11.5  19.9   NaN  12.0
2     30  11.5  11.8  28.8  12.1  12.2
3     40  12.0  12.2  37.7  12.6  12.4
4     50  12.3  12.4  46.6  13.7  13.2
5     60  12.6  12.7  55.5  14.0  14.1
6     70  13.0  13.1  64.4   NaN   NaN
7     80  13.8  14.1  73.3   NaN   NaN

根据评论,舍入对于实际的OP似乎不起作用 数据。要调试问题,请找到一些应该合并的行:

subframes = []
for frame in [examplelog, log2]:
    mask = (frame['Depth'] < 20.051) & (frame['Depth'] >= 20.0)
    subframes.append(frame.loc[mask])

然后发布

for frame in subframes:
    print(frame.to_dict('list')) 
    print(frame.info())          # shows the dtypes of the columns

这可能会为我们提供重现问题所需的信息。