gpt4 book ai didi

python - 没有转置数据帧的 Pandas 线图

转载 作者:行者123 更新时间:2023-12-05 05:07:50 29 4
gpt4 key购买 nike

我有一个 pandas 数据框,看起来像这样 -

    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

但是,我的原始数据很大,转置时间太长。还有其他方法可以实现吗?

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

最佳答案

如果您的数据量很大,您可能无论如何都看不到折线图上的任何内容...

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

关于python - 没有转置数据帧的 Pandas 线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58812886/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com