熊猫多个数据框图

时间:2019-10-14 15:29:07

标签: python pandas dataframe

我有两个数据帧。它们具有相同的结构,但是它们来自两个不同的模型。基本上,我想将它们进行比较以找到差异。我想做的第一件事是绘制两行,第一行来自第一个数据帧,第二行来自另一行。

这是我的工作: 我读了两个csv文件,

PRICES   = pd.read_csv('test_model_1.csv',sep=';',index_col=0, header = 0)
PRICES_B = pd.read_csv('bench_mark.csv',sep=';',index_col=0, header = 0)

然后我将两者的第8列绘制为:

rowM  = PRICES.iloc[8]
rowB  = PRICES_B.iloc[8]
rowM.plot()
rowB.plot()

这似乎不是正确的方法。实际上,我无法选择标签或图例。

此结果: comparison between the 8th row of the first dataframe and the 8th row of the second dataframe

有人可以建议我比较两个数据框并绘制一些选定列的正确方法吗?

1 个答案:

答案 0 :(得分:0)

让我们准备一些测试数据:

mtx1 = np.random.rand(10,8)*1.1+2
mtx2 = np.random.rand(10,8)+2

df1 = pd.DataFrame(mtx1)
df2 = pd.DataFrame(mtx2)

df1的示例输出:

Out[60]: 
              0         1         2         3
    0  2.604748  2.233979  2.575730  2.491230
    1  3.005079  2.984622  2.745642  2.082218
    2  2.577554  3.001736  2.560687  2.838092
    3  2.342114  2.435438  2.449978  2.984128
    4  2.416953  2.124780  2.476963  2.766410
    5  2.468492  2.662972  2.975939  3.026482
    6  2.738153  3.024694  2.916784  2.988288
    7  2.082538  3.030582  2.959201  2.438686
    8  2.917811  2.798586  2.648060  2.991314
    9  2.133571  2.162194  2.085843  2.927913

现在让我们来绘制它:

import matplotlib.pyplot as plt
%matplotlib inline

i = range(0,len(df1.loc[6,:]))   # from 0 to 3
plt.plot(i,df1.loc[6,:]) # take whole row 6
plt.plot(i,df2.loc[6,:]) # take whole row 6

结果: enter image description here