【问题标题】:In Pandas : How to check a list elements is Greater than a Dataframe Columns Values在 Pandas 中:如何检查列表元素是否大于数据框列值
【发布时间】:2021-11-11 10:50:16
【问题描述】:

我有一个名为 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)

【问题讨论】:

  • 如果你把它改成Option_dataframe['OPTION_TYPE'] == 'CE',你会得到同样的结果吗?
  • 你能发送一个类定义,其中 OPTION_TYPE 是原始的吗?因为这似乎是问题的根源...... 编辑:正如@chitown88 所说,您实际上并没有使用熊猫列 df[column_name] 获取列/值的方法。在访问其值时使用Option_dataframe['OPTION_TYPE']
  • 对不起,我稍微改变一下问题。我在这里遇到错误(option_df.CLOSE.apply(lambda x : x >= Base_prices))

标签: python pandas list compare


【解决方案1】:
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)的匹配索引。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-10
    • 2021-06-26
    • 2021-08-19
    • 1970-01-01
    • 2020-12-31
    • 2018-05-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多