【问题标题】:How to iterate over the columns of a dataframe and plot columns using a for loop in Python如何在 Python 中使用 for 循环遍历数据框的列并绘制列
【发布时间】:2021-06-13 19:42:54
【问题描述】:

我想通过在 for 循环中迭代列的索引来绘制数据框的列。我想将第二列与第三列进行对比,并在数据框中使用第三列的名称保存图形(我的第一列只是行的索引)。我想使用列的索引而不是列名来执行此操作。

这是我尝试过的:

import pandas as pd
from matplotlib import pyplot as plt

alldata = pd.read_csv('alldata.csv')

x=alldata.ix[:, 2::2]
y=alldata.iy[:, 1::2]

for i, column in alldata.iteritems():
    xi = x[i]
    for k, column in force.iteritems():
        yi = y[k]
        plt.plot(xi, yi)
        plt.show()

【问题讨论】:

    标签: python pandas dataframe numpy indexing


    【解决方案1】:
    alldata=pd.read_csv("alldata.csv")
    
    x=alldata.iloc[:, 1]
    y=alldata.iloc[:, 2]
    
    # or plt.scatter(x, y)
    for i in range(len(x)):
        plt.plot(x[i], y[i], marker=".")
    
    fig=plt.gcf()
    p="".join([alldata.columns[2], ".png"])
    fig.savefig(p)
    

    alldata.csv 看起来像

       index  secondcol  thirdcol
    0      1          1         6
    1      2          8         4
    2      3          3         9
    3      4          5         4
    4      5          4         6
    5      6          6         4
    6      7          7         8
    7      8          2         3
    

    thirdcol.png 看起来像

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-26
      • 1970-01-01
      • 2019-01-15
      • 1970-01-01
      • 2018-05-30
      相关资源
      最近更新 更多