-2

Suppose I have the following Pandas df:

import pandas as pd

df = pd.DataFrame( data = {'Day': ['2020-08-30', '2020-08-30','2020-08-30','2020-08-30',
                                        '2020-08-29', '2020-08-29','2020-08-29','2020-08-29',
                                            '2020-08-28', '2020-08-28','2020-08-28','2020-08-28'],
                                'Curve': ['Brazil', 'Japan','Brazil', 'Japan','Brazil', 'Japan','Brazil', 'Japan','Brazil', 'Japan','Brazil', 'Japan'],
                                'Value': [100, 950, 200, 1000, 50, 50, 250, 1200, 20, 30, 240, 1100],
                                'Expiry': ['1Y', '1Y', '2Y','2Y','1Y','1Y','2Y','2Y', '1Y','1Y','2Y','2Y']})

df['Difference'] = df.groupby(['Curve', 'Expiry']).Value.diff(-1)

Example Dataframe

I would like to plot it and have it result in the following graph, would anyone know how to do that? Basically, to plot the "difference" by "Curve", where the X axis is the "Expiry". I don't care about the legends, axis-name, etc. Just to understand how to plot the curves by expiry.

Desired Pyplot graph

Desired Pyplot graph

Also interested if its possible to know how to plot the curves in two separate graphs (i.e, one graph for each separate curve). Thanks in advance and have a good day

2 Answers2

0

You are looking for a pivot operation. But first it seems you want to filter only the last date.

df2 = (
    df[df.Day=='2020-08-30']
    .pivot(index='Expiry', columns='Curve', values='Difference')
)
print(df2)

df2.plot()
plt.show()

Output

Curve   Brazil  Japan
Expiry
1Y        50.0  900.0
2Y       -50.0 -200.0


Update

You can get the last date with df.Day.max() and you can select lines you want to plot with df.filter.

df.sort_values('Day', ascending=False, inplace=True)
df2 = (
    df[df.Day==df.Day.max()]
    .pivot(index='Expiry', columns='Curve', values='Difference')
)

only_columns = ['Brazil', 'North Korea']
df2 = df2.filter(items=only_columns, axis=1)
print(df2)

Output

Curve   Brazil
Expiry
1Y        50.0
2Y       -50.0
RichieV
  • 5,103
  • 2
  • 11
  • 24
  • Thanks Richie, your answer is helpful. Only if you want (you already helped a lot), do you know how would it go to plot different graphs for each separate "Curve"? – Pepini2341 Sep 04 '20 at 15:49
  • @Pepini2341 see the second answer in https://stackoverflow.com/q/22483588/6692898 – RichieV Sep 04 '20 at 15:52
  • @Pepini2341 I don't think anyone here would mind helping with a little clarification like that, but it is good getting used to searching around... I like googling "stackoverflow.com my query" to get better results... then if you still don't find the right answer by all means, comment or ask a new question... most likely it is just a matter of one not finding the right words to phrase our question – RichieV Sep 04 '20 at 15:55
0

hi you get the desired result like this

df2=df[["Difference","Day","Curve"]]
braz=df2[df2['Curve']=='Brazil']
jap=df2[df2['Curve']=='Japan']

braz=df2[df2['Curve']=='Brazil']
jap=df2[df2['Curve']=='Japan']


f, axes  = plt.subplots(1)

axes.plot(list(range(braz.shape[0])),braz["Difference"],label="Brazil")
axes.plot(list(range(braz.shape[0])),jap["Difference"],label="Japan")

axes.legend()
axes.set_xlabel('Difference')
axes.set_ylabel('?')


f.set_figheight(8)
f.set_figwidth(8)