简体   繁体   中英

pandas - user choose columns for graphs

So I need to make a program that reads an excel file and auto makes some comparative graphs with the columns the user choose, but I'm stuck on the list part, how can I make the user choose from the list of columns and make pandas recognize the user input choices? Is there any function or library to make this easier for me?

Sorry if this is a dumb question, I'm still new to this.

This is my code for now:

import pandas as pd
import os
import sys

def choose_file():
  global filepath
  filepath = input('Enter filepath: ')   
  assert os.path.exists(filepath), "I did not find the file at, " + str(filepath) #checks if file exists
  f = open(filepath, 'r+')
  print("We found your file.")
  f.close()

def open_file():
  global archive
  archive = pd.read_csv(filepath, encoding='latin1')  #pandas dataframe
  print(archive)

def choose_columns():
  print(archive.columns)
  #I'm stuck here and I don't know what to do
  

ok, so I looked around and found the solution to the part where the user has to list the columns he wants in this site: https://medium.com/thebit/lists-booleans-user-input-how-to-create-a-grocery-list-in-python-44454eba58df

def choose_columns():
  column_list = []
  print(archive.columns)
  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('Your final list: ', column_list)

Now I only need to figure out how to make pandas associate the column_list created by the user with the dataframe columns and use plotly to make the graphs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM