3

I have a pandas dataframe df with two columns (type and IR) as this one:

   type    IR
0   a      0.1
1   b      0.3
2   b      0.2
3   c      0.8
4   c      0.5
      ...

I want to plot three distributions (one for each type) with the values of the IR so, I write:

sns.displot(df, kind="kde", hue='type', rug=True)

but I get this error: The following variable cannot be assigned with wide-form data 'hue'

Any idea?


EDIT:

My real dataframe looks like

pd.DataFrame({"type": ["IR orig", "IR orig", "IR orig", "IR trans", "IR trans", "IR trans", "IR perm", "IR perm", "IR perm", "IR perm", "IR perm"],
              "IR": [1.41, 1.42, 1.32, 0.0, 0.44, 0.0, 1.41, 1.31, 1.41, 1.37, 1.34]
})

but with sns.displot(df, x='IR', kind="kde", hue='type', rug=True) I got ValueError: cannot reindex on an axis with duplicate labels

Palinuro
  • 184
  • 1
  • 1
  • 15

1 Answers1

2

Use:

df = pd.DataFrame({'type': {0: 'a', 1: 'b', 2: 'b', 3: 'c', 4: 'c'}, 
                   'IR': {0: 0.1, 1: 0.3, 2: 0.2, 3: 0.8, 4: 0.5}})
print (df)
  type   IR
0    a  0.1
1    b  0.3
2    b  0.2
3    c  0.8
4    c  0.5

sns.displot(df.reset_index(drop=True), x='IR', kind="kde", hue='type', rug=True)

pic

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • Thanks, but I got `ValueError: cannot reindex on an axis with duplicate labels` – Palinuro Jan 21 '23 at 20:10
  • @Palinuro - I add sample data to answer, can you test it? – jezrael Jan 21 '23 at 20:15
  • Noticeably, the example works with your solution when I run it on ipython but not in my code where I get `ValueError: cannot reindex on an axis with duplicate labels`. I've just edited my question with this. – Palinuro Jan 21 '23 at 20:32
  • 4
    @Palinuro You could try `sns.displot(df.reset_index(), x='IR', kind="kde", hue='type', rug=True)`. Some versions of seaborn/pandas have a problem when the index contains some repeated values. – JohanC Jan 21 '23 at 20:41
  • 1
    @JohanC - Thank you, I use `sns.displot(df.reset_index(drop=True), x='IR', kind="kde", hue='type', rug=True)` – jezrael Jan 21 '23 at 20:43
  • @jezrael, when I try to adjust my code to your answer I get the exact same error message as before (the one the original poster posted), any idea why? – bernando_vialli Jul 27 '23 at 21:19