0

I have a dataframe, which has different rates for multiple 'N' currencies over a time period.

dataframe
Dates      AUD     CAD    CHF    GBP    EUR
20/05/2019 0.11   -0.25  -0.98   0.63   0.96
21/05/2019 0.14   -0.35  -0.92   1.92   0.92
...
02/01/2020 0.135  -0.99  -1.4    0.93   0.83

Firstly, I would like to reshape the dataframe table to look like the below as I would like to join another table which would be in a similar format:

dataframe
Dates      Pairs   Rates
20/05/2019 AUD     0.11 
20/05/2019 CAD    -0.25
20/05/2019 CHF    -0.98
...       

...
02/01/2020 AUD    0.135
02/01/2020 CAD    -0.99
02/01/2020 CHF    -1.4   

Then, for every N currency, I would like to plot a histogram . So with the above, it would be 5 separate histograms based off each N ccy. I assume I would need to get this in some sort of loop, but not sure on the easiest way to approach. Thanks

pablo144
  • 117
  • 2
  • 8
  • 1
    for the reshaping use pandas melt https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.melt.html – sammywemmy Feb 19 '20 at 09:11

1 Answers1

1

Use DataFrame.melt first:

df['Dates'] = pd.to_datetime(df['Dates'], dayfirst=True)

df = df.melt('Dates', var_name='Pairs', value_name='Rates')
print (df)
        Dates Pairs  Rates
0  2019-05-20   AUD  0.110
1  2019-05-21   AUD  0.140
2  2020-01-02   AUD  0.135
3  2019-05-20   CAD -0.250
4  2019-05-21   CAD -0.350
5  2020-01-02   CAD -0.990
6  2019-05-20   CHF -0.980
7  2019-05-21   CHF -0.920
8  2020-01-02   CHF -1.400
9  2019-05-20   GBP  0.630
10 2019-05-21   GBP  1.920
11 2020-01-02   GBP  0.930
12 2019-05-20   EUR  0.960
13 2019-05-21   EUR  0.920
14 2020-01-02   EUR  0.830

And then DataFrameGroupBy.hist:

df.groupby('Pairs').hist()
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • Great this worked for me. Is there anyway though to add the "Pair" as a title for each individual chart? – pablo144 Feb 19 '20 at 15:04