Python Pandas Df.loc Not Being Found In A Table
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 pan
Solution 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
Solution 2:
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']]
Post a Comment for "Python Pandas Df.loc Not Being Found In A Table"