簡體   English   中英

如何使用 Python 中的 for 循環遍歷 dataframe 和 plot 列的列

[英]How to iterate over the columns of a dataframe and plot columns using a for loop in Python

我想通過在 for 循環中迭代列的索引來 plot dataframe 的列。 我想將第二列 plot 與第三列對比,並用 dataframe 中的第三列名稱保存圖形(我的第一列只是行的索引)。 我想使用列的索引而不是列名來執行此操作。

這是我嘗試過的:

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()
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 看起來像

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM