如何从列等于值的数据帧中进行切片

2024-04-16 04:35:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两组csv数据。其中一个包含两列(时间和一个布尔标志),另一个数据集包含一些信息,我有一些图形函数Id想要直观地显示。数据以不同的频率采样,因此数据集的行数可能不匹配。如何为布尔值为真的一系列数据绘制单独的图形?你知道吗

以下是联系人数据:

INDEX | TIME | CONTACT
0 | 240:18:59:31.750 | 0
1 | 240:18:59:32.000 | 0
2 | 240:18:59:32.250 | 0
........
1421 | 240:19:05:27.000 | 1
1422 | 240:19:05:27.250 | 1 

其他(车辆)数据并不重要,但包含重量、速度(MPH)、踏板位置等值

我有许多单独的大型excel文件,由于形状不匹配,我不确定如何使用时间标记切片数据,所以我在下面创建了一个函数来创建范围,但我认为这可以更容易地完成。你知道吗

下面是工作代码(输出如下)。简而言之,有没有更简单的方法?你知道吗

def determineContactSlices(data):
contactStart = None
contactEnd = None
slices = pd.DataFrame([])
for index, row in data.iterrows():
    if row['CONTACT'] == 1:
        # begin slice
        if contactStart is None:
            contactStart = index
            continue
        else:
            # still valid, move onto next
            continue
    elif row['CONTACT'] == 0:
        if contactStart is not None:
            contactEnd = index - 1
            # create slice and add the df to list
            slice = data[contactStart:contactEnd]
            print(slice)
            slices = slices.append(slice)
             # then reset everything
            slice = None
            contactStart = None
            contactEnd = None
            continue
        else:
            # move onto next row
            continue
return slices

输出:([15542行x 2列])

Index Time  CONTACT
1421   240:19:05:27.000        1
1422   240:19:05:27.250        1
1423   240:19:05:27.500        1
1424   240:19:05:27.750        1
1425   240:19:05:28.000        1
1426   240:19:05:28.250        1

            ...      ...
56815  240:22:56:15.500        1
56816  240:22:56:15.750        1
56817  240:22:56:16.000        1
56818  240:22:56:16.250        1
56819  240:22:56:16.500        1

有了这个输出,我打算遍历每个时间片,并在子块中显示车辆数据。你知道吗

任何帮助或指导都将不胜感激(:

更新:

我相信我可以只做filteredData = vehicleData[contactData['CONTACT'] == 1],但是当出现断开连接时,我面临着如何进行单独的绘图。例如,如果有7个连接在不同的时间和长度,我想有7个单独的绘图。你知道吗


Tags: 数据函数none图形dataindexif时间
1条回答
网友
1楼 · 发布于 2024-04-16 04:35:41

我认为你想做的是相对简单的,尽管我不确定我是否理解你想要的输出或者你想要在你得到它之后用它做什么。例如:

contact_df = data[data['CONTACT'] == 1]
non_contact_df = data[data['CONTACT'] == 0]

如果这没有帮助,请提供一些额外的细节,比如输出应该是什么样子,以及创建后您打算如何处理它。你知道吗

相关问题 更多 >