zach_hutz
zach_hutz

Reputation: 119

Get X Y value from Panda Dataframe

I'm trying to make this dataframe spit out the "x" value and "y" value and I'm completely stuck. I'm not sure how to go about this and the end goal is to make it plot the X and Y values on a graph, I'm just not sure how I would go about extracting these values. When I do iloc to try and get this information I get the indexes and while this isn't necessarily a problem it just added to my overall confusion. Here's one of my files and either a solution or explanation on how to do it would be extremely appreciated, I keep confusing myself more and more. Thanks so much

EDIT: I got better clarification and realize in this example month would be the x and average would be the y. So I know I can do data.iloc to get these values one by one, but is there a way I could add this to like a for loop for example to iterate through all the values one by one and then have them all get plotted on a graph? I cant think of the best way to accomplish this

from flask import render_template, request, redirect
from app import app
import os
import csv
import pandas as pd

@app.route('/', methods=["GET", "POST"])
def index():
    data = []
    if request.method == 'POST':
        if request.files:
            uploaded_file = request.files['filename']
            filepath = os.path.join(app.config['FILE_UPLOADS'], uploaded_file.filename)
            uploaded_file.save(filepath)
            with open(filepath) as file:
                csv_file = csv.reader(file)
                for row in csv_file:
                    data.append(row)
            data = pd.DataFrame(data)
            print(data)
            return render_template('index.html', tables=[data.to_html(classes='data')], titles=data.columns.values, header=False, index=False)
    return render_template('index.html', data=data)


@app.route('/help')
def help():
    return render_template('help.html')

app.config['FILE_UPLOADS'] = "C:\\Users\\Zachary\\Documents\\VSCode_Projects\\monday_webapp\\app\\static\\file\\uploads"

This is a sample CSV I'm using

"Month", "Average"
"May",  0.1
"Jun",  0.5
"Jul",  0.7
"Aug",  2.3
"Sep",  3.5
"Oct",  2.0
"Nov",  0.5
"Dec",  0.0

Upvotes: 2

Views: 10889

Answers (2)

zach_hutz
zach_hutz

Reputation: 119

The solution I came up with is by doing

d_list = list(data.columns.values)

        x_label = d_list[0]
        y_label = d_list[1]

This allows me to do plt.plot(data[x_label], data[y_label]) I'm essentially turning the column names into a list and pulling out the first and second string and applying that to my dataframe.

Upvotes: 0

Zuhair Abid
Zuhair Abid

Reputation: 189

You can do like this:

x= df["month"]
y=df["average"]

plt.plot(x,y)

Upvotes: 3

Related Questions