1

I created this table using pandas' groupby function and want to extract each column as vector/array
df_duration_means = df.groupby('Duration').mean()

Interest Loan amount LTV
Duration
6 0.107500 274000.000000 0.652500
9 0.112500 510500.000000 0.580000
12 0.105345 276632.758621 0.595517
15 0.080000 81000.000000 0.678000
18 0.109167 516557.666667 0.455867
24 0.101500 374500.000000 0.554800

Now I want to extract a vector for each of the 4 columns (including duration). But I was not able to do it, even checking pandas documentation and all possible similar threads.

dur = df_duration_means.index
print(dur)
interest_mx = df_duration_means['Loan amount']
print(interest_mx)

So that I can plot each column vector vs the duration vector:

fig =  plt.figure()
ax = fig.add_axes([0,0,1,1])
ax.plot(dur,in,color=)
Wurschti
  • 23
  • 2
  • 9

1 Answers1

1

Try .reset_index() first, and then use method .tolist(). Like this:

df_duration_means = df_duration_means.reset_index()
duration = df_duration_means.index.tolist()
interest = df_duration_means['Interest'].tolist()
loan_amount = df_duration_means['Loan amount'].tolist()
ltv = df_duration_means['LTV'].tolist()

And then, use duration, interest, loan_amount, and ltv as an input into plotly

EDIT: There is simpler solution using plotly.express:

import plotly.express as px 
df_duration_means = df_duration_means.reset_index()
fig = px.line(df_duration_means, x=df_duration_means.index, y=['Interest', 'Loan amount', 'LTV'])
Punker
  • 1,770
  • 9
  • 16
  • Cool, thank you. The first one worked out quite well! For the second one I had issues with Jupyter recognizing plotly. For anyone having the same issue, here is a corresponding thread (that did not help me :D): https://stackoverflow.com/questions/36959782/plotly-in-jupyter-issue – Wurschti Jan 28 '21 at 10:36