我想回圈洗掉行,直到到達時間值為 的行04:30:00,然后停止洗掉程序。我怎么做?
這是我的資料示例:
ticker date time vol vwap open high low close
0 AACG 2022-01-06 04:07:00 242 2.0400 2.04 2.04 2.04 2.04
1 AACG 2022-01-06 04:08:00 427 2.0858 2.06 2.10 2.06 2.10
2 AACG 2022-01-06 04:09:00 906 2.1098 2.10 2.11 2.10 2.11
3 AACG 2022-01-06 04:16:00 186 2.1108 2.12 2.12 2.10 2.10
4 AACG 2022-01-06 04:30:00 237 2.0584 2.06 2.06 2.06 2.06
5 AACG 2022-01-06 04:31:00 700 2.1098 2.10 2.11 2.10 2.11
我試過了,但它沒有顯示任何改變:
row = 0
while df['time'].values[row] == datetime.time(4, 30) == False:
print(df['time'].values[row])
df.drop(row, axis=0, inplace=True)
row = row 1
這是df.info():
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 10 entries, 0 to 9
Data columns (total 12 columns):
ticker 10 non-null object
date 10 non-null object
time 10 non-null object
vol 10 non-null int64
vwap 10 non-null float64
open 10 non-null float64
high 10 non-null float64
low 10 non-null float64
close 10 non-null float64
lbh 10 non-null int64
lah 10 non-null int64
trades 10 non-null int64
dtypes: float64(5), int64(4), object(3)
memory usage: 1.1 KB
更新:再次感謝大家的幫助。
df[df['time'] >= datetime.time(4, 30)] 幫助我洗掉了不必要的行。
uj5u.com熱心網友回復:
您可以使用布爾掩碼對資料進行切片。如果你df['time']是 datetime.time 物件,那么你可以df簡單地切片:
out = df[df['time'] > datetime.time(4,30)]
輸出:
ticker date time vol vwap open high low close
5 AACG 2022-01-06 04:31:00 700 2.1098 2.1 2.11 2.1 2.11
uj5u.com熱心網友回復:
不要回圈,而是切片。您可以為此使用掩碼(此處使用布爾陣列和 生成cummax):
df[df['time'].eq('04:30:00').cummax()]
輸出:
ticker date time vol vwap open high low close
4 AACG 2022-01-06 04:30:00 237 2.0584 2.06 2.06 2.06 2.06
5 AACG 2022-01-06 04:31:00 700 2.1098 2.10 2.11 2.10 2.11
如果您還想排除匹配的行:
df[df['time'].eq('04:30:00').shift(fill_value=False).cummax()]
uj5u.com熱心網友回復:
如果將time列轉換為 a ,則此處不需要回圈TimedeltaIndex:
out = df[~pd.to_timedelta(df['time']).lt('04:30:00')]
print(out)
# Output
ticker date time vol vwap open high low close
4 AACG 2022-01-06 04:30:00 237 2.0584 2.06 2.06 2.06 2.06
5 AACG 2022-01-06 04:31:00 700 2.1098 2.10 2.11 2.10 2.11
它有效嗎?
from datetime import time
out = df[df['time'] >= time(4, 30)]
print(out)
# Output:
ticker date time vol vwap open high low close
4 AACG 2022-01-06 04:30:00 237 2.0584 2.06 2.06 2.06 2.06
5 AACG 2022-01-06 04:31:00 700 2.1098 2.10 2.11 2.10 2.11
# Info
print(df['time'].iloc[0])
# datetime.time(4, 7)
uj5u.com熱心網友回復:
這個解決方案
- 將
"date"和"time"列組合成一個新datetime.datetime列, - 搜索給定時間的第一次出現,并且
- 洗掉直到但不包括給定時間的第一次出現的行。
- 如果未找到給定時間,則不會洗掉任何內容,因為
row_ix_of_first_occurrence將等于0。 - 該解決方案適用于整數索引和字串索引。
from io import StringIO
from datetime import datetime
import pandas as pd
def parse_date_time(date: str, time_24: str) -> datetime:
return datetime.strptime(" ".join((date, time_24)), "%Y-%m-%d %H:%M:%S")
df = pd.read_csv(
StringIO("""ticker date time vol vwap open high low close
AACG 2022-01-06 04:07:00 242 2.0400 2.04 2.04 2.04 2.04
AACG 2022-01-06 04:08:00 427 2.0858 2.06 2.10 2.06 2.10
AACG 2022-01-06 04:09:00 906 2.1098 2.10 2.11 2.10 2.11
AACG 2022-01-06 04:16:00 186 2.1108 2.12 2.12 2.10 2.10
AACG 2022-01-06 04:30:00 237 2.0584 2.06 2.06 2.06 2.06
AACG 2022-01-06 04:31:00 700 2.1098 2.10 2.11 2.10 2.11"""),
delim_whitespace=True,
parse_dates={"datetime": ["date", "time"]},
date_parser=parse_date_time,
index_col=False,
header=0,
engine="python",
keep_date_col=False,
)
print(f"DataFrame initially:\n{df.to_string()}\n")
is_given_time = (
(df["datetime"].dt.hour == 4)
& (df["datetime"].dt.minute == 30)
& (df["datetime"].dt.second == 0)
)
row_ix_of_first_occurrence = is_given_time.argmax()
row_ix_delete = df.index[:row_ix_of_first_occurrence]
df = df.drop(index=row_ix_delete)
print(f"DataFrame after filtering:\n{df.to_string()}")
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/409476.html
標籤:
