pandasのデータフレームにおけるループ処理の効率向上
https://teratail.com/questions/298065
のつづき
python
1import io
2import pandas as pd
3
4data = """
5Symbol,Exchange,CurrentPrice,CurrentPriceTime
63276,1,1402,00:00.6
73276,1,1402,00:00.7
83276,1,1402,00:00.9
93276,1,1402,00:01.1
103276,1,1403,00:01.4
113276,1,1403,00:01.8
123276,1,1403,00:01.9
133276,1,1406,00:02.1
143276,1,1401,00:02.5
153276,1,1401,00:02.8
163276,1,1417,00:03.0
173276,1,1417,00:03.2
183276,1,1417,00:03.8
193276,1,1417,00:04.4
203276,1,1417,00:05.2
213276,1,1417,00:05.6
223276,1,1417,00:06.3
23"""
24
25df = pd.read_csv(io.StringIO(data))
26
27df["CurrentPriceTime"] = "00:" + df["CurrentPriceTime"]
28df["CurrentPriceTime"] = pd.to_timedelta(df["CurrentPriceTime"])
29
30df.dtypes
31
32df1 = df.loc[:, ["CurrentPrice", "CurrentPriceTime"]]
33
34df_diff = df1.diff()
35
36df_diff["nextDirect"] = df_diff["CurrentPrice"].apply(
37 (lambda x: 1 if x < 0 else 2 if x > 0 else 3 if x == 0 else None)
38)
39df_diff["nextDirect"] = df_diff["nextDirect"].mask(
40 (df_diff["CurrentPriceTime"] < pd.Timedelta(seconds=30))
41 & (df_diff["nextDirect"] == 3)
42)
43
44df_diff["nextDirect"].fillna(method="bfill")
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/10/21 15:33