Python KeyError:“;[&[';';';';]中的任何一个都不在[列]”;熊猫蟒蛇
我需要做一个程序,读取excel文件,采取用户输入的列,并作出一些简单的图形,我不关心轴的比例。以下是我目前的代码:Python KeyError:“;[&[';';';';]中的任何一个都不在[列]”;熊猫蟒蛇,python,pandas,plotly,Python,Pandas,Plotly,我需要做一个程序,读取excel文件,采取用户输入的列,并作出一些简单的图形,我不关心轴的比例。以下是我目前的代码: import pandas as pd import sys import os import plotly.express as px import plotly.graph_objects as go 这将使用户输入excel文件并将其转换为dataframe def choose_file(): global filepath filepath = input('
import pandas as pd
import sys
import os
import plotly.express as px
import plotly.graph_objects as go
这将使用户输入excel文件并将其转换为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)
这将使用户选择列并将它们放入列表中
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()
我一直在使用来自Kaggle的著名数据集Titanic,在choose_columns()中,我选择了“PassengerID”和“Surved”两列。但我得到了一个错误:
`KeyError: "None of [Index([('PassengerId', 'Survived')], dtype='object')] are in the [columns]"
我如何解决这个问题,有人能向我解释为什么会发生这种情况吗
KeyError: "None of [Index([('PassengerId', 'Survived')], dtype='object')] are in the [columns]"
根据错误,您只有一列
('PassengerId','surved')
啊,我明白了。那么我该如何将它们分开呢?@Moon你是如何创建该列的?在创造中把它们分开。