After reading the CSV file in pandas, creating a list and summarizing the averages, I got an error and I don't know if the file is the cause or the code.
If there is a solution, please let me know.
code
import pandas as pd
# Load csv in Pandas
df=pd.read_csv("kion10y.csv", encoding="utf-8")
# list temperatures by date
md = {}
for i,row in df.iterrows():
m,d,v = (int(row['month']), int(row['day']), float(row['temperature']))
key=str(m)+"/"+str(d)
if not (key in md): md [key] = []
md[key]+=[v]
# calculate the average for each date
avs = {}
for key in md:
v=avs[key]=sum(md[key])/len(md[key])
print("{0}:{1}".format(key,v))
error message
Traceback (most recent call last):
File"/Users/fujiokamasaya/Desktop/python.test/lib/python 3.7/site-packages/pandas/core/indexes/base.py", line 4411, inget_value
return libindex.get_value_at(s,key)
File "pandas/_libs/index.pyx", line 44, inpandas._libs.index.get_value_at
File "pandas/_libs/index.pyx", line 45, inpandas._libs.index.get_value_at
File "pandas/_libs/util.pxd", line 98, in pandas._libs.util.get_value_at
File "pandas/_libs/util.pxd", line 83, in pandas._libs.util.validate_indexer
TypeError: 'str' object cannot be interpreted as an integer
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File"/Users/fujiokamasaya/PycharmProjects/project/tenki.1.py", line 10, in <module>
m,d,v = (int(row['month']), int(row['day']), float(row['temperature']))
File"/Users/fujiokamasaya/Desktop/python.test/lib/python 3.7/site-packages/pandas/core/series.py", line 871, in__getitem__
result=self.index.get_value(self,key)
File"/Users/fujiokamasaya/Desktop/python.test/lib/python 3.7/site-packages/pandas/core/indexes/base.py", line 4419, get_value
raise e1
File"/Users/fujiokamasaya/Desktop/python.test/lib/python 3.7/site-packages/pandas/core/indexes/base.py", line 4405, inget_value
return self._engine.get_value(s,k,tz=getattr(series.dtype, "tz", None))
File "pandas/_libs/index.pyx", line 80, inpandas._libs.index.IndexEngine.get_value
File "pandas/_libs/index.pyx", line 90, inpandas._libs.index.IndexEngine.get_value
File "pandas/_libs/index.pyx", line 138, inpandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 1618, inpandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 1626, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'Month'
CSV File Contents
year, month, day, temperature, quality, homogeneous,
2010,1,5,7.4,8,1
2010,1,6,4.6,8,1
2010,1,7,4.5,8,1
2010,1,8,5.5,8,1
2010,1,9,5.8,8,1
2010,1,10,7.3,8,1
2010,1,11,7.0,8,1
2010,1,12,6.8,8,1
2010,1,13,2.5,8,1
2010,1,14,2.3,8,1
2010,1,15,4.6,8,1
2010,1,16,3.9,8,1
2010,1,17,3.9,8,1
2010,1,18,5.4,8,1
2010,1,19,7.9,8,1
2010,1,20,9.9,8,1
2010,1,21,10.3,8,1
2010,1,22,6.5,8,1
The file is expected to be the cause.
Check csv for the month
column.
As @metropolis commented, it seems that the reason is that there is a space in the csv header.
Enable skipinitialspace
by referring to official documentation or commentary blog.
Month, Sunday, Temperature
1,2,3
1,2,6
# 1/2 : 4.5
Chinese soft-shelled turtle, day, temperature
1,2,3
1,2,6
Month, Sunday, Temperature
1,2,3
1,2,6
import pandas as pd
# get into error
# df = pd.read_csv("kion10y.csv", encoding = "utf-8")
# error-free
df=pd.read_csv("kion10y.csv", encoding="utf-8", skipinitialspace=True)
md = {}
for i,row in df.iterrows():
m,d,v = (int(row['month']), int(row['day']), float(row['temperature']))
key=str(m)+"/"+str(d)
if not (key in md):
md [key] = [ ]
md[key]+=[v]
avs = {}
for key in md:
v=avs[key]=sum(md[key])/len(md[key])
print("{0}:{1}".format(key,v))
There is a space in front of the header from month to homogeneous.
year, month, day, temperature, quality, homogeneous,
Try deleting the space.
year, month, day, temperature, quality, homogeneous,
366 To Limit Column Values to Strings in a String List Using sqlalchemy
356 Unity Virtual Stick Does Not Return to Center When You Release Your Finger
345 Who developed the "avformat-59.dll" that comes with FFmpeg?
340 Memory layouts learned in theory don't work out as expected when actually printed as addresses.
© 2023 OneMinuteCode. All rights reserved.