我怎样才能创建一个函数 Python
How would I be able to create a function Python
我想在现有代码中添加一段代码,检查 High
列值是否大于列 Open, Low, Close
的行的其他值。我还想检查一下 Low
列的行值是否低于其他列 Open, High, Close
列的行值。因此,在 High
列的值的第 1 行不符合此条件,因为 Open and Close
值高于它,因此它在预期输出中。所以基本上这段代码应该检查每个 High
值是否仍然是最大值并且 Low
值是每一行的最低值。
代码:
import pandas as pd
import numpy as np
import time
import datetime
A =[[1645661520000, 37352.0, 37276.5, 37252.0, 37376.0, 15.56119087],
[1645661580000, 37376.0, 37414.0, 37376.0, 37314.0, 49.38248589],
[1645661640000, 37414.0, 37414.0, 37350.0, 37350.0, 45.70306699],
[1645661700000, 37350.0, 37374.0, 37350.0, 37373.5, 14.4306948],
[1645661760000, 37373.5, 36588.0, 37373.5, 37388.0, 3.59340947],
[1645661820000, 37388.0, 37388.0, 37388.0, 39388.0, 21.45525727]]
column_names = ["Unix","Open", "High","Low", "Close", "Volume"]
df = pd.DataFrame(A, columns=column_names)
df.insert(1,"Date", pd.to_datetime(df["Unix"].to_numpy()/1000,unit='s'))
display(df)
预期输出:
Rows: 1, 5, 6
#Row 1 is low column is the highest of all than all the other rows
#Row 5 the High column is lower than the other columns
#Row 6 the low column is higher than the other columns
试试这个:
# columns of interest
val_cols = ['Open', 'High', 'Low', 'Close']
# This is where High condition is violated
high_cond_violated = df['High'] < df[val_cols].max(axis=1)
# This is where Low condition is violated
low_cond_violated = df['Low'] > df[val_cols].min(axis=1)
# These are the indices where either condition is violated
(df.index[high_cond_violated | low_cond_violated] + 1).values.tolist()
输出:
[1, 2, 5, 6]
你可以分别调用,看看到底是哪一个违规了eg
# Only High condition violated
(df.index[high_cond_violated] + 1).values.tolist()
...
我想在现有代码中添加一段代码,检查 High
列值是否大于列 Open, Low, Close
的行的其他值。我还想检查一下 Low
列的行值是否低于其他列 Open, High, Close
列的行值。因此,在 High
列的值的第 1 行不符合此条件,因为 Open and Close
值高于它,因此它在预期输出中。所以基本上这段代码应该检查每个 High
值是否仍然是最大值并且 Low
值是每一行的最低值。
代码:
import pandas as pd
import numpy as np
import time
import datetime
A =[[1645661520000, 37352.0, 37276.5, 37252.0, 37376.0, 15.56119087],
[1645661580000, 37376.0, 37414.0, 37376.0, 37314.0, 49.38248589],
[1645661640000, 37414.0, 37414.0, 37350.0, 37350.0, 45.70306699],
[1645661700000, 37350.0, 37374.0, 37350.0, 37373.5, 14.4306948],
[1645661760000, 37373.5, 36588.0, 37373.5, 37388.0, 3.59340947],
[1645661820000, 37388.0, 37388.0, 37388.0, 39388.0, 21.45525727]]
column_names = ["Unix","Open", "High","Low", "Close", "Volume"]
df = pd.DataFrame(A, columns=column_names)
df.insert(1,"Date", pd.to_datetime(df["Unix"].to_numpy()/1000,unit='s'))
display(df)
预期输出:
Rows: 1, 5, 6
#Row 1 is low column is the highest of all than all the other rows
#Row 5 the High column is lower than the other columns
#Row 6 the low column is higher than the other columns
试试这个:
# columns of interest
val_cols = ['Open', 'High', 'Low', 'Close']
# This is where High condition is violated
high_cond_violated = df['High'] < df[val_cols].max(axis=1)
# This is where Low condition is violated
low_cond_violated = df['Low'] > df[val_cols].min(axis=1)
# These are the indices where either condition is violated
(df.index[high_cond_violated | low_cond_violated] + 1).values.tolist()
输出:
[1, 2, 5, 6]
你可以分别调用,看看到底是哪一个违规了eg
# Only High condition violated
(df.index[high_cond_violated] + 1).values.tolist()
...