Home > other >  get the full row data from the found extrema
get the full row data from the found extrema

Time:10-16

I am new to using pandas and I can't find a way to get the full row of the found extrema

df = pd.read_csv('test.csv')

df['min'] = df.iloc[argrelextrema(df.Close.values, np.less_equal,
                                  order=10)[0]]['Close']
df['max'] = df.iloc[argrelextrema(df.Close.values, np.greater_equal,
                                  order=10)[0]]['Close']
# create lists for `min` and `max`
min_values_list = df['min'].dropna().tolist()
max_values_list = df['max'].dropna().tolist()

print(min_values_list, max_values_list)

It print only the minima and extrema values, but I need the full row data of found minima / maxima

Example of data:

Datetime,Date,Open,High,Low,Close
2021-01-11 00:00:00 00:00,18638.0,1.2189176082611084,1.2199585437774658,1.2186205387115479,1.2192147970199585

CodePudding user response:

If the list is required, then I would suggest:

def df_to_list_rowwise(df: pd.DataFrame) -> list:
    return [df.iloc[_, :].tolist() for _ in range(df.shape[0])]


df_min_values = df.iloc[argrelextrema(np.array(df.Close), np.less_equal)[0], :]
df_max_values = df.iloc[argrelextrema(np.array(df.Close), np.greater_equal)[0], :]


print(df_to_list_rowwise(df_min_values))
print(df_to_list_rowwise(df_max_values))

Would that help?

CodePudding user response:

try to use df.dropna().index.tolist() instead of specifying the column because adding the column name returns just the value of a specific row and the specified column not the whole row

  • Related