pairplot columns from multiple dataframes labelled by classes from the category column

Viewed 1159

I am not sure how to do this but I believe is doable. I have three dataframes having the same column definition but dataset from different years. I then want to pairplot the numeric columns taking the columns one-by-one and plotting the data from these dfs appropriately labeling the set for which data comes from. The objective is to understand the pattern in data for each column as compared by year.

I illustrate what I mean with these 2 dataframes where dataset in df1 is from year 2018 and df2 from year 2019 respectively:

df1
          id      speed    accelaration      jerk      mode
0          1      1.94     -1.01             1.05      foot
1          1      0.93      0.04            -0.17      foot
2          3      0.50     -0.16             0.05      bike
3          3      0.57      0.05             0.19      bike
4          5      3.25     -0.13            -0.09      bus
5          5      0.50     -0.25             0.25      bus
6          5      0.25      0.10             0.25      bus

df2
          id    speed   accelaration      jerk      mode
0         17      1.5      0.00           0.00      foot
1         17      1.5      0.00          -0.30      foot
2         17      1.5     -0.30           0.06      foot
3         15     4.55      0.01          -0.36      bike
4         15     4.57     -0.35           0.02      bike
5         87     9.82     -0.29          -0.12      bus
6         87     8.65     -0.78           0.07      bus

Ignoring the id column, I would like to get a result that looks like this figure (this is just an illustration of the intended result that I draw):

enter image description here

Simply calling sns.pairplot() twice for each df will not give the intended result, as I did that:

sns.pairplot(df1, vars=df1.columns[1:4], hue='mode')
sns.pairplot(df2, vars=df2.columns[1:4], hue='mode')
plt.show()

enter image description here

Can someone help describe how the intended answer could be obtained from this?

1 Answers
  • Given the dataframe, add a 'year' column to each one
  • Use pandas.concat to combine the dataframes
    • Reset the index, but don't drop it. The index will be used as the x-axis, since one hasn't been provided. This will maintain the relative position of the data from each dataframe
  • Stack 'speed', 'acceleration', and 'jerk' into one column, 'event', to create a long, tidy format, dataframe.
  • Plot the data with a seaborn.FacetGrid and mapped with a seaborn.scatterplot.
    • The 'index' column, not the dfl.index, is used as the x-axis.
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

# add year to the dataframes
df1['year'] = 2018
df2['year'] = 2019

# combine the dataframes
df = pd.concat([df1, df2]).reset_index()

# stack the dataframe into a long (tidy) format
dfl = df.set_index(['index', 'id', 'mode', 'year']).stack().reset_index().rename(columns={'level_4': 'event', 0: 'value'})

# display(dfl)
   index  id  mode  year         event  value
0      0   1  foot  2018         speed   1.94
1      0   1  foot  2018  accelaration  -1.01
2      0   1  foot  2018          jerk   1.05
3      1   1  foot  2018         speed   0.93
4      1   1  foot  2018  accelaration   0.04
5      1   1  foot  2018          jerk  -0.17
6      2   3  bike  2018         speed   0.50
7      2   3  bike  2018  accelaration  -0.16
8      2   3  bike  2018          jerk   0.05
9      3   3  bike  2018         speed   0.57


# plot a FacetGrid mapped with a scatterplot
g = sns.FacetGrid(data=dfl, row='event', col='mode', hue='year')
g.map(sns.scatterplot, 'index', 'value').add_legend(bbox_to_anchor=(1, 0.5), loc='center left')
g.fig.tight_layout()

enter image description here

Related