0

I am quite new to pandas, so please excuse any simple errors I may not have caught or understood properly.

I'm trying to find a certain row and column in a given .csv file with pandas. Attached below is the table for reference. Table info

The table goes from 1/1/2015 to 12/31/2017. I'm trying to locate a specific month and year row based on user input, as well as locate the Temp High, Temp Avg, and Temp Low columns. I'm using a day of 1 just as a placeholder. From looking around, I've tried to use

months = {'January': 1, 'February': 2, 'March': 3, 'April': 4, 'May': 5, 'June': 6,
      'July': 7, 'August': 8, 'September': 9, 'October': 10, 'November': 11, 'December': 12}
month = str(input('Enter a month: '))
year = str(input('Enter a year: '))
day = 1
print('')


find = df.loc[[str(months[month]) + '/' + str(day) + '/' + str(year)], ['Temp High', 'Temp Avg', 
'Temp Low']]

to find the info I'm looking for, but this results in ""None of [Index(['6/1/2012'], dtype='object')] are in the [index]"". How do I go about fixing this? Any help is appreciated.

Astro
  • 1
  • 2

2 Answers2

1

You don't have your date column set as an index in your data frame. To set any data frame column as an index, use the below snippet:

df.set_index("ColumnName", inplace = True)

Pandas set_index() is a method to set a list, Series, or Data frame as an index of a data frame.

Syntax:

Dataframe.set_index(keys, drop=True, append=False, inplace=False, verify_integrity=False)

Parameters

  • keys: Column name or list of column name
  • drop: Boolean value which drops the column used for index if True
  • append: Appends the column to existing index column if True
  • inplace: Makes the changes in the dataframe if True
  • verify_integrity: Checks the new index column for duplicates if True
Dharman
  • 30,962
  • 25
  • 85
  • 135
Mohit Khaitan
  • 113
  • 11
0

You can filter without using loc, instead using cell values

find = df[df.Date == str(months[month]) + '/' + str(day) + '/' + str(year)][['Temp High', 'Temp Avg']]

OR assign 'Date' column as index and use then use loc

df1 = df.set_index(['Date'])
find = df1.loc[[str(months[month]) + '/' + str(day) + '/' + str(year)], ['Temp High', 'Temp Avg']]