Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pandas: Finding maxima of 2d data (integers) in Dataframe

I have a 2d data set of (x,y). x and y are integer values.

How can I use only Pandas code to find all x values where y reaches its maximum values (there are multiple and same absolute maxima)?

I also want to plot (with pandas.DataFrame.plot) x vs. y and mark the maxima positions.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Example code:

import numpy as np
import pandas as pd

np.random.seed(10)

x = np.arange(100)*0.2
y = np.random.randint(0, 20, size=100)

data = np.vstack((x, y)).T

df = pd.DataFrame(data, columns=['x', 'y'])

ymax = df['y'].max()

df_ymax = df[df['y'] == ymax]

print(df_ymax)

#        x     y
# 13   2.6  19.0
# 24   4.8  19.0
# 28   5.6  19.0
# 86  17.2  19.0
# 88  17.6  19.0

df.plot(x='x', y='y', figsize=(8, 4),
        ylabel='y', legend=False, style=['b-'])

I have no idea how to mark the maxima values (df_ymax) in the same plot, e.g. using circles. How can that be solved?

enter image description here

The final plot should look like this (here I programmed everything with numpy and matplotlib):

enter image description here

>Solution :

Get the Axes returned by df.plot and reuse it to plot the maxima values:

ax = df.plot(x='x', y='y', figsize=(8, 4), ylabel='y', legend=False, style=['b-'])
df_ymax.plot.scatter(x='x', y='y', color='r', ax=ax)

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading