i try to generate a new dataframe (expected_result) , which is based on two existing dataframes (rank, value) and a filter. both existing datafames have the same header and index. the new dataframe should show the value of the dataframe "value" if the value in the dataframe "rank" is equal or less than the filter_var. if the ranking is higher than the filter_var = show "nan"
import pandas as pd
#df 1 Rank
ranking = {'Stock A':[1, 1, 1, 1],
'Stock B':[3, 3, 4, 4],
'Stock C':[4, 4, 3, 2],
'Stock D':[2, 2, 2, 3],
}
rank = pd.DataFrame(ranking)
#df 2 values
values = {'Stock A':[101, 102, 103, 104],
'Stock B':[99, 99, 99 , 99],
'Stock C':[99, 98, 100, 103],
'Stock D':[100, 100, 100, 102],
}
value = pd.DataFrame(values)
#Filter
filter_var = 2 #better or equal
#expecet result
results = {'Stock A':[101, 102, 103, 104],
'Stock B':['nan', 'nan', 'nan' , 'nan'],
'Stock C':['nan', 'nan', 'nan', 103],
'Stock D':[100, 100, 100, 'nan'],
}
#example results
expected_result = pd.DataFrame(results)
thanks for help