pythonpandasmplfinance

Pandas Loc selection to a list of NaN and Values


I want to select some points from a pandas data frame df under a certain condition and then add them to a plot using mplfinance. Im using the mpf.make_addplot() function. This takes a list in the form of:

signal=[nan, nan, 7, nan, 6, 8, nan]

However selecting values from a pandas df gives a list like so:

df = pd.DataFrame({'column': [1, 2, 7, 0, 6, 8, -9]
pandaslist =  df.loc[ df.column > 5 ]
pandaslist = [7,6,8]

(Ofcourse the pandaslist has an index) Is there a way to use pandas loc to get a list in form of the signal list? Is there a way to make mpf to accept input in the style of the pandaslist?


Solution

  • .loc doesn't work like that.

    Instead, you can use df.where() to select the values you want while nullifying the others.

    df.where(df['column'] > 5)
    
       column
    0     NaN
    1     NaN
    2     7.0
    3     NaN
    4     6.0
    5     8.0
    6     NaN