简体   繁体   English

从 pandas groupby 中提取列作为向量

[英]Extracting columns as vectors from pandas groupby

I created this table using pandas' groupby function and want to extract each column as vector/array我使用 pandas 的 groupby function 创建了这个表,并希望将每一列提取为向量/数组
df_duration_means = df.groupby('Duration').mean()

Interest兴趣 Loan amount贷款额度 LTV生命周期价值
Duration期间
6 6 0.107500 0.107500 274000.000000 274000.000000 0.652500 0.652500
9 9 0.112500 0.112500 510500.000000 510500.000000 0.580000 0.580000
12 12 0.105345 0.105345 276632.758621 276632.758621 0.595517 0.595517
15 15 0.080000 0.080000 81000.000000 81000.000000 0.678000 0.678000
18 18 0.109167 0.109167 516557.666667 516557.666667 0.455867 0.455867
24 24 0.101500 0.101500 374500.000000 374500.000000 0.554800 0.554800

Now I want to extract a vector for each of the 4 columns (including duration).现在我想为 4 列(包括持续时间)中的每一列提取一个向量。 But I was not able to do it, even checking pandas documentation and all possible similar threads.但我无法做到这一点,甚至检查 pandas 文档和所有可能的类似线程。

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:这样我就可以 plot 每个列向量与持续时间向量:

fig =  plt.figure()
ax = fig.add_axes([0,0,1,1])
ax.plot(dur,in,color=)

Try .reset_index() first, and then use method .tolist() .首先尝试.reset_index() ,然后使用方法.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然后,使用durationinterestloan_amountltv作为 plotly 的输入

EDIT: There is simpler solution using plotly.express:编辑:使用 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'])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM