簡體   English   中英

在 Pandas 中:如何檢查列表元素是否大於數據框列值

[英]In Pandas : How to check a list elements is Greater than a Dataframe Columns Values

我有一個名稱為 Base_price 的列表

def Base_price(Future_price):
    Base_prices = []
    for Future_prices in Future_price:
        def nearest_multiple(base: float, num: float) -> float:
            return base * int(num / base)
        Base_price = nearest_multiple(50, int(Multiplier * Future_prices))
        Base_prices.append(Base_price)
    return Base_prices

Base_prices = Base_price(Future_price)

在此處輸入圖片說明

我想將此列表元素與數據框進行比較。 我有相同數量的數據幀Len = 19 (Dataframes and list elements)

def Option_read_files():
    global Option_dataframe
    Option_dataframe = []
    for files in dir_list:
        extension = "pkl"
        os.chdir(path1)
        load = []
        for i in glob.glob(files.format(extension)):
            load.append(path1+"\\"+i)
        for i in load:
            Option_df = pd.read_pickle(i)
            Min_Option = Option_df.EXPIRY.min()
            Option_dataset = Option_df[(Option_df.EXPIRY == Min_Option)]
#             print(Option_dataset)
            Option_dataframe.append(Option_dataset)
            
    return Option_dataframe
Option_dataframe = Option_read_files()

在此處輸入圖片說明

數據幀也存儲在列表中。

現在我想檢查

index 0 Base_price <= index 0 dataframe 
index 1 Base_price <= index 1 dataframe 
index 2 Base_price <= index 2 dataframe 
index 3 Base_price <= index 3 dataframe 
..
.. So on.

這是我遇到問題的代碼,

def Option_filtering(Option_dataframe, Base_prices):
    for option_df in Option_dataframe:
        df_ce = option_df[(option_df.OPTION_TYPE == 'CE') & (option_df.CLOSE.apply(lambda x : x >= Base_prices)) & (option_df.TIME <= Start_time)]
        df_ce_start_time = df_ce[df_ce.TIME == Start_time]
        df_ce_start_time = df_ce_start_time.sort_values(by=['CLOSE'])
        Instrument_ce = df_ce_start_time.iloc[0]    
        Strike_ce = Instrument_ce.STRIKE_PRICE
        Expiry_ce = Instrument_ce.EXPIRY
        Option_ce = Instrument_ce.OPTION_TYPE
        df_ce_strike = df_ce[df_ce.STRIKE_PRICE == Strike_ce]
        df_ce_low = df_ce_strike.LOW.min()
        print("Call Min of Low",df_ce_low)

Option_filtering(Option_dataframe, Base_prices)

在此處輸入圖片說明

def Option_filtering(Option_dataframe, Base_prices):
    Ce_low, Ce_strike = [], []
    for i in range(len(Option_dataframe)):
        option_df = Option_dataframe[i]
        df_ce = option_df[(option_df.OPTION_TYPE == 'CE') & 
                          (option_df.CLOSE >= Base_prices[i]) & 
                          (option_df.TIME <= Start_time)]
        df_ce_start_time = df_ce[df_ce.TIME == Start_time]
        df_ce_start_time = df_ce_start_time.sort_values(by=['CLOSE'])
        Instrument_ce = df_ce_start_time.iloc[0]    
        Strike_ce = Instrument_ce.STRIKE_PRICE
        Expiry_ce = Instrument_ce.EXPIRY
        Option_ce = Instrument_ce.OPTION_TYPE
        df_ce_strike = df_ce[df_ce.STRIKE_PRICE == Strike_ce]
        df_ce_low = df_ce_strike.LOW.min()
        Ce_low.append(df_ce_low)
        Ce_strike.append(Strike_ce)
#         print("Call Min of Low",df_ce_low)
        
    return Ce_low, Ce_strike

Ce_low, Ce_strike = Option_filtering(Option_dataframe, Base_prices)

我使用i作為兩個列表(數據庫和 Base_price)的匹配索引。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM