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

How can I straighten a curve in pandas?

This is my dataframe where "Original" is what I have and "Target" is where I would like to straighten it approximately to:

df = pd.DataFrame({
    'Original': [0, 6, 14, 23, 49, 100],
    'Target': [0, 10, 25, 40, 65, 100],
    'Linear': [0, 20, 40, 60, 80, 100]
})
df.plot()

How can I also define the degree of straightening, like Target is 45% straightened and Linear is 100% straightened (and Original 0% straightened)?

plot

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

>Solution :

Interpolate between your original data and the linear data. Empirically, a weight of 0.475 gives a pretty close approximation.

import pandas as pd
from matplotlib import pyplot as plt

df = pd.DataFrame(
    {
        "Original": [0, 6, 14, 23, 49, 100],
        "Target": [0, 10, 25, 40, 65, 100],
        "Linear": [0, 20, 40, 60, 80, 100],
    }
)
weight = 0.475
df[f"Weight_{weight}"] = df["Original"] * (1 - weight) + df["Linear"] * weight
df.plot()
plt.show()

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