Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用“应用”删除组中的重复项_Python_Duplicates_Pandas - Fatal编程技术网

Python 使用“应用”删除组中的重复项

Python 使用“应用”删除组中的重复项,python,duplicates,pandas,Python,Duplicates,Pandas,我已经成功地通过它们的临时近邻合并了两个数据帧。我当前的中间结果如下所示: merge_key jd var2 index distance 2010-01-01 00:00:00 0 2455197.500000 0 2010-01-01 00:00:00 0 2010-01-01 00:06:00 0 2455197.500000

我已经成功地通过它们的临时近邻合并了两个数据帧。我当前的中间结果如下所示:

                     merge_key              jd  var2               index  distance  
2010-01-01 00:00:00          0  2455197.500000     0 2010-01-01 00:00:00      0
2010-01-01 00:06:00          0  2455197.500000     0 2010-01-01 00:00:00   -360
2010-01-01 00:12:00          0  2455197.500000     0 2010-01-01 00:00:00   -720
2010-01-01 00:18:00          1  2455197.517361     1 2010-01-01 00:25:00    420
2010-01-01 00:24:00          1  2455197.517361     1 2010-01-01 00:25:00     60
2010-01-01 00:30:00          1  2455197.517361     1 2010-01-01 00:25:00   -300
2010-01-01 00:36:00          1  2455197.517361     1 2010-01-01 00:25:00   -660
2010-01-01 00:42:00          2  2455197.534722     2 2010-01-01 00:50:00    480
2010-01-01 00:48:00          2  2455197.534722     2 2010-01-01 00:50:00    120
2010-01-01 00:54:00          2  2455197.534722     2 2010-01-01 00:50:00   -240
在下一步中,我将删除重复的条目,并仅选择那些具有最小距离的条目。我想到了:

df.groupby("merge_key").apply(lambda x: x.ix[np.abs(x['distance']).idxmin()])
然而,这导致:

          merge_key       jd var2                index distance
merge_key                                                      
0                 0  2455198    0  2010-01-01 00:00:00        0
1                 1  2455198    1  2010-01-01 00:25:00       60
2                 2  2455198    2  2010-01-01 00:50:00      120
似乎“jd”的数据类型已更改为整数?我也不想把merge_键作为新索引

我期望的输出实际上是:

                     merge_key              jd  var2               index  distance  
2010-01-01 00:00:00          0  2455197.500000     0 2010-01-01 00:00:00      0
2010-01-01 00:24:00          1  2455197.517361     1 2010-01-01 00:25:00     60
2010-01-01 00:48:00          2  2455197.534722     2 2010-01-01 00:50:00    120

如果使用稍微简单的方法执行此操作,则会得到正确的结果:

In [11]: g = df.groupby('merge_key')

In [12]: min_dists = g.distance.apply(lambda x: x.abs().idxmin())

In [13]: min_dists
Out[13]:
merge_key
0            0
1            4
2            8
dtype: int64

In [14]: df.iloc[min_dists]
Out[14]:
                  date  merge_key              jd  var2                index  distance
0  2010-01-01 00:00:00          0  2455197.500000     0  2010-01-01 00:00:00         0
4  2010-01-01 00:24:00          1  2455197.517361     1  2010-01-01 00:25:00        60
8  2010-01-01 00:48:00          2  2455197.534722     2  2010-01-01 00:50:00       120

我认为这可能是一个bug,因此可能值得打开。

非常感谢!作为旁注,您使用了连续的数字作为索引,而我使用了日期作为索引,这意味着在
min\u dist
中不是位置而是日期。然后必须使用函数
df.ix[min\u dists]
。在本例中,read\u table和StringIO将此数据读入df的正确语法是什么?谢谢我不确定我是否明白你的意思,但是如果你问我如何从上面的问题中得到这个数据帧,答案是
read\u clipboard
:)我将表复制并粘贴到一个名为raw的字符串中。然后我尝试了df=pd.read_table的各种组合(StringIO(raw)、header=True、delim_whitespace=True),但我没有得到它。谢谢您的回复。@julieth Ah由于日期包含空格(您不想将其用作分隔符),所以我想我使用了
read_剪贴板(sep='\s\s+)
(至少两个空格)。否则,我认为您的StringIO解决方案也会起作用。