Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
281 views
in Technique[技术] by (71.8m points)

python - Extracting columns as vectors from pandas groupby

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
question from:https://stackoverflow.com/questions/65927748/extracting-columns-as-vectors-from-pandas-groupby

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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'])

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...