熊猫线图而无需转换数据框

我有一个像这样的熊猫数据框-

    Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec Marks
0   30  31  29  15  30  30  30  50  30  30  30  26  Student1
1   45  45  45  45  41  45  35  45  45  45  37  45  Student2
2   21  11  21  21  21  21  21  21  21  21  17  21  Student3
3   30  30  33  30  30  30  50  30  30  30  22  30  Student4
4   39  34  34  34  34  34  23  34  40  34  34  34  Student5
5   41  41  41  28  41  56  41  41  41  41  41  41  Student6

如果我按如下所示转置数据,则可以绘制折线图

    Marks   Student1    Student2    Student3    Student4    Student5    Student6
0   Jan       30          45          21          30          39           41
1   Feb       31          45          11          30          34           41
2   Mar       29          45          21          33          34           41
3   Apr       15          45          21          30          34           28
4   May       30          41          21          30          34           41
5   Jun       30          45          21          30          34           56
6   Jul       30          35          21          50          23           41
7   Aug       50          45          21          30          34           41
8   Sep       30          45          21          30          40           41
9   Oct       30          45          21          30          34           41
10  Nov       30          37          17          22          34           41
11  Dec       26          45          21          30          34           41

但是,我的原始数据非常庞大,转置它花费的时间太长。还有其他方法可以实现这一目标吗?

请注意-这只是我为简单起见而创建的虚拟数据框,我的原始数据非常复杂且庞大。

quanta1 回答:熊猫线图而无需转换数据框

如果您的数据量巨大,那么无论如何您都将无法在线路图上看到任何东西...

import matplotlib.pyplot as plt
import pandas as pd
from io import StringIO
import numpy as np

df = pd.read_table(StringIO("""    Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec Marks
0   30  31  29  15  30  30  30  50  30  30  30  26  Student1
1   45  45  45  45  41  45  35  45  45  45  37  45  Student2
2   21  11  21  21  21  21  21  21  21  21  17  21  Student3
3   30  30  33  30  30  30  50  30  30  30  22  30  Student4
4   39  34  34  34  34  34  23  34  40  34  34  34  Student5
5   41  41  41  28  41  56  41  41  41  41  41  41  Student6"""),sep='\s+')


x = df.columns.tolist()[:-1]
y = df.iloc[:,:-1].values
for i,j in enumerate(y):
    plt.plot(x,j,label=df['Marks'].iloc[i])
plt.ylim(bottom=0)
plt.legend(loc='upper right')

enter image description here

本文链接:https://www.f2er.com/3119644.html

大家都在问