python 如何在不删除整行的情况下删除数组中的重复项?

bf1o4zei  于 2023-02-28  发布在  Python
关注(0)|答案(1)|浏览(155)

我正在寻找地震探测以及P波和S波。一旦找到这些参数,我就把它们存储在不同的表中:一个用于检测,一个用于P波,一个用于S波。

for i_sta in range(len(List_dir)):
    
    file = List_dir[i_sta]
    
    st = read(file)
    
    day = st[0].stats.starttime

    print(day.strftime('%Y-%m-%d'))
    
#-----------------
# Processing
#-----------------
    
    st.detrend()
    st.filter('bandpass', freqmin = 1, freqmax = 45, zerophase = 'True')
    
#-------------------------------------------
# Filter the trace for alternative plotting
#-------------------------------------------
    
    stfilt = st.copy()
    stfilt.filter('bandpass', freqmin = 1, freqmax = 15, zerophase = 'True')
    
#--------------------
# Model annotations
#--------------------
    
    annotations = model.annotate(st, strict = False)   # Returns data with annotations
    print(annotations)
    

#------------------------
# Picks and detections
#------------------------
    
    picks, detections = model.classify(st, detection_threshold = 0.9, P_threshold = 0.6, S_threshold = 0.6)   # Returns a list of picks and detections

    print("Picks:")
    for pick in picks:
        print(pick)

    print("\nDetections:")    
    for detection in detections:
        print(detection)
        
    #if picks and detections:
        
#----------------------------------------------------------
# Next lines to uncomment for writing results into a file
#----------------------------------------------------------   

    
    for detection in detections:
        A.append({
            "Date": day.strftime('%Y-%m-%d'),
            "Start_detection": detection.start_time.strftime('%H:%M:%S'),
            "End_detection": detection.end_time.strftime('%H:%M:%S'),
            "Detection_probability": detection.peak_value,
        })
            
    for pick in picks:
        if pick.phase == 'P':
             B.append({
                 "Date": day.strftime('%Y-%m-%d'),
                  "P_time": pick.start_time.strftime('%H:%M:%S'),
                  "P_probability": pick.peak_value,
             })
        else:
             C.append({
                 "Date": day.strftime('%Y-%m-%d'),
                 "S_time": pick.start_time.strftime('%H:%M:%S'),
                 "S_probability": pick.peak_value,
            })
    
    print("===============")
        
    del picks, detections, st, file

一旦得到这些表,我就合并这3个表,得到包含所有发现的地震事件的最终表。

A = pd.DataFrame(A)
B = pd.DataFrame(B)
C = pd.DataFrame(C)
all_evt = pd.merge(pd.merge(A, B, how ='left'), C, how = 'left')

然而,当我看最后的表格时,有重复项。更准确地说,重复项是为有几次检测或检测到P/S波的日子创建的。

你知不知道我怎样才能去掉这些重复项而不删除整行?
我想要的是一个表格,其中在同一时间(天和小时)检测到的检测和P/S波在同一行上,如果没有检测到检测或P/S波S,则会出现NaN。以下是预期结果:

我已经尝试了pd.merge的所有功能,但没有成功。然后我查看了dataframe.join甚至pd.concat,但我没有得到正确的结果。
提前感谢:)
下面是一个可重现的示例:

# --------------------------
# Writing results in lists
#---------------------------

A = [{'Date': '2020-09-23',
  'Start_detection': '08:01:52',
  'End_detection': '08:01:56',
  'Detection_probability': 0.9995621},
 {'Date': '2020-10-28',
  'Start_detection': '10:53:19',
  'End_detection': '10:53:23',
  'Detection_probability': 0.9973672},
 {'Date': '2020-10-29',
  'Start_detection': '11:12:18',
  'End_detection': '11:12:21',
  'Detection_probability': 0.9061912},
 {'Date': '2020-10-29',
  'Start_detection': '11:27:37',
  'End_detection': '11:27:42',
  'Detection_probability': 0.9933528},
 {'Date': '2020-10-29',
  'Start_detection': '11:47:18',
  'End_detection': '11:47:33',
  'Detection_probability': 0.9660959},
 {'Date': '2020-11-23',
  'Start_detection': '16:19:03',
  'End_detection': '16:19:06',
  'Detection_probability': 0.9979086}]

B = [{'Date': '2020-09-23', 'P_time': '08:01:52', 'P_probability': 0.7567052},
 {'Date': '2020-10-29', 'P_time': '11:12:17', 'P_probability': 0.6098999},
 {'Date': '2020-11-23', 'P_time': '16:19:03', 'P_probability': 0.8836917},
 {'Date': '2020-11-23', 'P_time': '19:21:58', 'P_probability': 0.8364198}]

C = [{'Date': '2020-09-23', 'S_time': '08:01:55', 'S_probability': 0.75159454},
 {'Date': '2020-11-23', 'S_time': '16:19:05', 'S_probability': 0.8108436},
 {'Date': '2020-11-23', 'S_time': '16:19:14', 'S_probability': 0.6465341},
 {'Date': '2020-11-23', 'S_time': '19:22:01', 'S_probability': 0.71296066}]

#------------------------------
# Convert data into dataframes
#------------------------------

A = pd.DataFrame(A)
B = pd.DataFrame(B)
C = pd.DataFrame(C)

#-------------------
# Merge dataframes
#-------------------
all_evt = pd.merge(pd.merge(A, B, how ='left'), C, how = 'left')
all_evt
rlcwz9us

rlcwz9us1#

我认为你高估了pd.merge在这里的作用。目前它合并 Dataframe A和B中所有同名的列,即Date列。你可以用pd.merge(A, B, on=['Date', ...])更具体一些。我理解你想做的是合并DateStart_detection < P_time < End_detection。因为我不认为pd.merge或任何panda函数都能做到这一点。这里还有两种方法可以解决这个问题:
第1步:我已经将所有事件添加到for pick in picks循环中的DataFrame A中。编写一个函数,将DataFrame A过滤为事件的日期,并针对Start_detection < P_time < End_detection写入此行。如果没有匹配的行,则添加一个新行。之后可以按DateStart_detection对DataFrame进行排序。
第2步:按照你的例子做,但是在all_evt中,用NaN替换P_timeP_probability中的所有值,其中Start_detection > P_timeEnd_detection < P_time。我想,考虑到没有落在一个范围内的事件会很困难。也许可以使用拆分-应用-合并的方法,并按日期做这件事?
我希望这能有所帮助,我还没有检查可复制的示例,但如果您需要进一步的线索,可能会这样做。

相关问题