KeyError even when key is present

hi community,
I want to write a program that gives me the options of reading a particular dataset from my local machine or reading it directly from the URL (data.world), but I got the error:
KeyError: 'order_date'
I understand this error is supposedly because there isn’t a column called order_date’ but I’m pretty sure there is. My code snippet:

import streamlit as st

import plotly.express as px

import pandas as pd

import warnings

warnings.filterwarnings('ignore')

import plotly.figure_factory as ff

# file upload
fl = st.file_uploader(":file_folder: Upload a file", type=(["csv","txt","xlsx","xls"]))
if fl is not None:
    filename = fl.name
    st.write(filename)
    df = pd.read_csv(filename, encoding = "ISO-8859-1")
else:
    df = pd.read_excel('https://query.data.world/s/73qjpswpfbmeljx2k5ki6bbutw7qv6?dws=00000')

col1, col2 = st.columns((2))
df["order_date"] = pd.to_datetime(df["order_date"])

How do I resolve this, please?

I downloaded the excel file from the URL and it seems like the column is not named ‘order_date’ instead ‘Order Date’. So, change the code in last line to:
df["order_date"] = pd.to_datetime(df["Order Date"])

1 Like

Thanks. I somehow assumed the columns in the Excel file would be exactly the same as the dataset on the site. Thanks again!

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.