简体   繁体   中英

How i can filter a dataframe with another dataframe column, both have different index and few value in second dataframe?

I have one data frame file and a second data frame time . and file data frames has 30398 rows data and time data frames has 70 rows data.

file =

在此处输入图片说明

time =

在此处输入图片说明

And now I want to filter file.Time == time but it's giving me error -

Can only compare identically-labeled Series objects

在此处输入图片说明

My code -

def FilteringData():
    con_a, con_b, con_c = [], [], []
    min_expiry = file.EXPIRY.min()
    for i in time:
        print(i, "\n")
        condition_a = file[(file.EXPIRY == min_expiry) &
                          (file.OPTION_TYPE == op) &
                          (file.STRIKE_PRICE == str(main_strike)) &
                          (file.TIME == i)]
        condition_b = file[(file.EXPIRY == min_expiry) &
                          (file.OPTION_TYPE == op) &
                          (file.STRIKE_PRICE == str(add_strike)) &
                          (file.TIME == i)]
        condition_c = file[(file.EXPIRY == min_expiry) &
                          (file.OPTION_TYPE == op) &
                          (file.STRIKE_PRICE == str(sub_strike)) &
                          (file.TIME == i)]
        con_a.append(condition_a)
        con_b.append(condition_b)
        con_c.append(condition_c)
    return con_a, con_b, con_c   
con_a, con_b, con_c = FilteringData()

If you need to keep the rows of file in which the value for the column TIME exists in the time series, then you can simply do a slicing operation.

out = file[file['TIME'].isin(time)]

Another option is to do a merge so oniy records on both files are kept:

Out = file.merge(time.to_frame(),left_on='TIME',right_on='TIME')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM