Home > Software engineering >  Plot average of multiple line plots with different x values
Plot average of multiple line plots with different x values

Time:12-31

I have multiple dataframes that look similar to this:

x y     x    y
1 2     0.5  2
2 4     1.5  6
3 6     3    12

Where the x columns are my indices. I want to plot the average line plot for these multiple datasets. My idea was to concatenate the two dataframes so that I have a scatterplot and can do a best fit line, but Pandas is throwing an error Reindexing only valid with uniquely valued Index objects. I've read other questions for this error message and have renamed my index names and column names to x_1 x_2 and y_1 and y_2 but it is still complaining, I believe because some of the x values are the same. What am I doing wrong here?

CodePudding user response:

Not sure if I understand completely how your dataframes look like, but you can concatenate two (or more) dataframes df1,df2... by doing:

new_dataframe = pd.DataFrame(np.concatenate([df1,df2]),columns=['x','y'])

where my imports are

import pandas as pd
import numpy as np

CodePudding user response:

Are you just looking for a best fit line for all the points? If so you can concat and use lmplot.

import pandas as pd
import seaborn as sns

df = pd.DataFrame({'x':[1,2,3],'y':[2,4,6]})
df2 = pd.DataFrame({'x':[.5,1.5,3], 'y':[2,6,12]})

out = pd.concat([df,df2])

sns.lmplot(data=out, x='x', y='y', ci=None);

enter image description here

  • Related