![](/img/trans.png)
[英]Python Pandas, translating start-end events into single consolidated Dataframe
[英]Pandas: finding start-end values of consecutive indexes in a Pandas dataframe
我有一個看起來像這樣的數據框( main_df ):
value created_at
0 18.930542 2019-03-04 02:43:08 UTC
1 18.895210 2019-03-04 02:44:09 UTC
2 0.000000 2019-03-04 02:45:09 UTC
3 0.000000 2019-03-04 02:46:10 UTC
4 0.000000 2019-03-04 02:47:11 UTC
5 0.000000 2019-03-04 02:48:12 UTC
6 0.000000 2019-03-04 02:49:13 UTC
7 18.857025 2019-03-04 02:50:14 UTC
8 18.857025 2019-03-04 02:51:14 UTC
9 18.847290 2019-03-04 02:52:15 UTC
10 18.847290 2019-03-04 02:53:17 UTC
11 0.000000 2019-03-04 02:54:17 UTC
12 0.000000 2019-03-04 02:55:19 UTC
13 0.000000 2019-03-04 02:56:19 UTC
14 18.837677 2019-03-04 02:57:20 UTC
我想在“值”列中找到重復零值的段。 我知道如何定位零,只需這樣做:
zeros_df=main_df.loc[main_df['value'] == 0]
現在,我想得到的,但我不知道如何,只將每個連續索引系列的開始和開始保存到名為aux_df2的新數據幀中,並計算開始和結束之間的時間差以分鍾為單位的每個系列連續值。 我打算用這個做最后一部分(盡管我想成對地計算每個開始 - 結束對之間的差異):
aux_df2['t_diff'] = ['temp_index'].diff().astype('timedelta64[m]')
但是第一部分我不知道該怎么做。 我正在尋找的是使aux_df2看起來像這樣:
有人能幫助我嗎? 提前致謝。
編輯:回復@peer,這就是我生成數據框的方式。 請注意,上面的快照並不反映從 UTC 到 EST 的時區變化。
import pandas as pd
filepath=r'C:\Users\myfile.csv'
main_df=pd.read_csv(filepath)
main_df['created_at']=main_df['created_at'].apply(pd.to_datetime)
main_df['created_at'] = main_df['created_at'].dt.tz_localize('UTC').dt.tz_convert('EST')
您預期的t_diff
似乎不正確,但這里有一個使用np.ptp
的解決方案,它采用范圍內的maximum - minimum
和pandas.Series.cumsum
我使用cumsum
因為這樣我們可以找到為0
的行
df['cumsum'] = df.value.cumsum()
df['t_diff'] = df.groupby('cumsum').created_at.transform(np.ptp)
df.drop('cumsum', axis=1, inplace=True)
print(df)
Index value created_at t_diff
0 0 18.930542 2019-03-04 02:43:08 00:00:00
1 1 18.895210 2019-03-04 02:44:09 00:05:04
2 2 0.000000 2019-03-04 02:45:09 00:05:04
3 3 0.000000 2019-03-04 02:46:10 00:05:04
4 4 0.000000 2019-03-04 02:47:11 00:05:04
5 5 0.000000 2019-03-04 02:48:12 00:05:04
6 6 0.000000 2019-03-04 02:49:13 00:05:04
7 7 18.857025 2019-03-04 02:50:14 00:00:00
8 8 18.857025 2019-03-04 02:51:14 00:00:00
9 9 18.847290 2019-03-04 02:52:15 00:00:00
10 10 18.847290 2019-03-04 02:53:17 00:03:02
11 11 0.000000 2019-03-04 02:54:17 00:03:02
12 12 0.000000 2019-03-04 02:55:19 00:03:02
13 13 0.000000 2019-03-04 02:56:19 00:03:02
14 14 18.837677 2019-03-04 02:57:20 00:00:00
請注意,在這種情況下,我進行了轉換以獲取組中每一行旁邊的差異。
根據你提供的信息,我做了這樣的事情:
import pandas as pd
import numpy as np
df = pd.DataFrame({"index":[2,3,4,5,6,11,12,13],"value": [0,0,0,0,0,0,0,0]})
df["prev_index"] = df["index"].shift(1)
df["next_index"] = df["index"].shift(-1)
df["include"] = df.apply(lambda row: True if np.isnan(row.next_index) or np.isnan(row.prev_index)
else (True if abs(row["index"]-row.next_index) != 1
or abs(row["index"] - row.prev_index) != 1 else False),axis=1)
df[df["include"]][["index","value"]]
我從您提供的示例中創建了一個簡單的數據框,所以我的想法只是移動數據集以獲得上一個和下一個索引,基於兩列,我剛剛創建了一個布爾值來知道索引是否是我的索引尋找。 希望能幫助到你!
這里的游戲有點晚了,但我想提供我的解決方案來使用值的過濾和值的差異來找到開始和結束:
aux_df2 = main_df[
(main_df['value'] == 0) &
(
(main_df['value'].diff() != 0)
| main_df['value'].diff().shift(-1) != 0
)]
這給出:
value created_at
2 0.0 2019-03-04 02:45:09+00:00
6 0.0 2019-03-04 02:49:13+00:00
11 0.0 2019-03-04 02:54:17+00:00
13 0.0 2019-03-04 02:56:19+00:00
不過,我想不出一種優雅的方式來獲取 t_diff。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.