I need to make a program that read an excel file, take the columns by the user input and makes some simple graphs, I don't care about the axis scale. Here's my code for now:
import pandas as pd
import sys
import os
import plotly.express as px
import plotly.graph_objects as go
this is make the user input the excel file and convert it to pandas dataframe
def choose_file():
global filepath
filepath = input('Enter filepath: ')
assert os.path.exists(filepath), "I did not find the file at, " + str(filepath)
f = open(filepath, 'r+')
print("Hooray we found your file!")
f.close()
def open_file():
global file
file = pd.read_csv(filepath, encoding='latin1', delimiter=',')
print(file.columns)
this will make the user choose the columns and put them in a list
def choose_columns():
global column_list
global file
column_list = []
needs_items = True
while needs_items == True:
user_input = input('Select the columns: ')
column_list.append(user_input)
for user_input in column_list:
print('- ' + user_input)
answer = input("Add another item? (y/n) ")
if answer == "n":
needs_items = False
print('Final list: ', column_list)
file = file[column_list]
print(file)
def build_graph():
global file
df = file[[column_list]].plot()
df.show()
choose_file()
open_file()
choose_columns()
build_graph()
I have been using the famous dataset Titanic from Kaggle and in choose_columns() I pick the columns 'PassengerID' and 'Survived'. But I'm getting this error:
`KeyError: "None of [Index([('PassengerId', 'Survived')], dtype='object')] are in the [columns]"
How can I fix this and can someone explain to me why this is happening?