gpt4 book ai didi

python - 如何根据时间对齐数据?

转载 作者:行者123 更新时间:2023-11-28 18:58:20 25 4
gpt4 key购买 nike

我正在使用 pandas(python 库)来分析一组数据。我的工作是根据时间对齐这些数据。我会解释:我有不同的开关,如果按下或不按下,它们会给我 1 或 0,并且按下后,一段时间后会自行恢复到之前的状态,有不同的开关。这些数据与状态的日期和时间一起保存在 csv 文件中。我的目标是在这样的图表中查看一天的开关状态: https://ibb.co/RgJbq9J

可以在一天中的任何时间按下开关,我的问题是我无法对齐图表中的数据。

数据示例

datetime 1          switch 1    datetime 2      switch 2
08/12/2018 13:21:08 0 08/12/2018 10:15:59 1
08/12/2018 13:24:33 1 08/12/2018 10:18:13 0
08/12/2018 13:29:54 0 08/12/2018 10:29:28 1
08/12/2018 13:34:43 1 08/12/2018 10:31:37 0
08/12/2018 13:39:01 0 08/12/2018 10:34:01 1
08/12/2018 13:40:49 1 08/12/2018 10:36:14 0
08/12/2018 13:43:04 0 08/12/2018 10:37:05 1
08/12/2018 13:44:51 1 08/12/2018 10:39:19 0
08/12/2018 13:47:07 0 08/12/2018 10:40:03 1
08/12/2018 13:51:20 1 08/12/2018 10:42:15 0
08/12/2018 13:53:30 0 08/12/2018 10:42:51 1
08/12/2018 13:53:39 1 08/12/2018 10:45:14 0
08/12/2018 13:55:58 0 08/12/2018 10:52:29 1
08/12/2018 13:57:08 1 08/12/2018 10:54:49 0
08/12/2018 13:59:27 0 08/12/2018 11:01:01 1
08/12/2018 13:59:54 1 08/12/2018 11:05:32 0

output graph example

这就是我的目标,即在每日绘制的图表中显示所有开关。该图与上表数据无关。

最佳答案

好的,我添加了一个额外的值集来显示一点多样性:

                datetime 3  switch 3  
0 2018-08-12 08:13:00.000 0
1 2018-08-12 08:13:01.915 0
2 2018-08-12 08:13:40.607 1
3 2018-08-12 08:14:02.863 0
4 2018-08-12 08:14:51.945 1
5 2018-08-12 08:15:57.060 0
6 2018-08-12 08:16:39.584 1
7 2018-08-12 08:16:48.351 1
8 2018-08-12 08:17:55.674 1
9 2018-08-12 08:18:46.208 0
10 2018-08-12 08:20:00.030 1
11 2018-08-12 08:20:02.992 0
12 2018-08-12 08:21:20.673 1
13 2018-08-12 08:22:29.867 1
14 2018-08-12 08:23:04.670 0
15 2018-08-12 08:23:54.177 0

脚本:

import os
import pandas as pd
import matplotlib.pyplot as plt

root = r'C:\Users\...\...'
file_name = 'test_file.xlsx'
full_path = os.path.join(root, file_name)

### Import data
df = pd.read_excel(full_path)

### Get our switch columns
switch_cols = [i for i in df.columns.values.tolist() if i.startswith('switch')]

### Subset our main dataframe to include only switch columns
df1 = df.reindex(columns=switch_cols).copy()


def plot_results(dataframe):
### Get swtich column names into a list
y_cols = [i for i in dataframe.columns.values.tolist()]

### Make the x-axis value set our dataframe axis values
x_vals = dataframe.index.values.tolist()

### Create subplots based on the numer of swtich columns
fig, axs = plt.subplots(len(y_cols), 1, sharex=True)

### Remove horizontal space between axes
fig.subplots_adjust(hspace = 0)

### Iterate over enumerated list of switch columns
for i, v in enumerate(switch_cols):
### set axes to plot values from a swtich set;
### Set drawstyle to 'steps-pre'
axs[i].plot(x_vals, dataframe[v].values, drawstyle='steps-pre')

### Add padding to y-axis limits
axs[i].set_ylim(-0.1, 1.1)

### Set y-axis label to switch column label
axs[i].set_ylabel(v)

### Plot results
plt.show()

plot_results(df1)

输出:

enter image description here

关于python - 如何根据时间对齐数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55677010/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com