0

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?

1 Answer 1

1
KeyError: "None of [Index([('PassengerId', 'Survived')], dtype='object')] are in the [columns]"

According to the error, you have only one column ('PassengerId', 'Survived').

Sign up to request clarification or add additional context in comments.

2 Comments

ah, I see. So how do I separate them?
@Moon How do you create that column? Separate them in creation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.