0
#read the data in csv
df_sales = pd.read_csv("C:\\Users\\Yuvraj\\Downloads\\sales_data.csv",'utf-8')
#convert date field from string to datetime
df_sales = {1: "date", 2: "store", 3: "item", 4:"sales"}
df_sales['date'] = pd.to_datetime(df_sales['date'])

#show first 10 rows
df_sales.head(10)

was trying to convert 'date' field from string to datetime

This is the output : KeyError: 'date'

what do I do?

  • First you read a data frame into `df_sales` and in the second step you overwrite it with a dict that only contains the keys 1, 2 and 3. Does this help? https://stackoverflow.com/a/34092032/18667225 – Markus Feb 18 '23 at 20:50

1 Answers1

0

'date' is not in your dict as key, it's a value of key = 1.

So, you can't do df_sales["date"], it raises KeyError.

Mathieu B.
  • 51
  • 4