Issue
given a data set such as: link to data: I want to compare and find the maximas of the avg glucose, and once I find the maximas I want to be able to compute the rate of glucose utilization
The error occurs when I write
if(df1['Avg. Glucose (mg/dL)'][i] > df1['Avg. Glucose (mg/dL)'][i + 1]):
in :
import pandas as pd
# reading sheet 1
df = pd.read_excel('data.xlsx', sheet_name=1)
# removing all rows with null Avg Glucose values and putting it into new data frame
df1 = df.dropna(subset=["Avg. Glucose (mg/dL)"])
# iterrating through the rows of new data frame
for i in df1.index:
# after day 10
if(df1['Days of Culture'][i] > 10):
#if the value is greater than the next
if(df1['Avg. Glucose (mg/dL)'][i] > df1['Avg. Glucose (mg/dL)'][i + 1]):
print(df1['Avg. Glucose (mg/dL)'][i])
Getting this error:
Traceback (most recent call last):
File "C:\Users\Abrar Mahi\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\indexes\base.py", line 3080, in get_loc
return self._engine.get_loc(casted_key)
File "pandas\_libs\index.pyx", line 70, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 101, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 1625, in pandas._libs.hashtable.Int64HashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 1632, in pandas._libs.hashtable.Int64HashTable.get_item
KeyError: 13
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "c:/Users/Abrar Mahi/Desktop/Biomilq/BioMilq.py", line 14, in <module>
if(df1['Avg. Glucose (mg/dL)'][i] > df1['Avg. Glucose (mg/dL)'][i + 1]):
File "C:\Users\Abrar Mahi\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\series.py", line 853, in __getitem__
return self._get_value(key)
File "C:\Users\Abrar Mahi\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\series.py", line 961, in _get_value
loc = self.index.get_loc(label)
File "C:\Users\Abrar Mahi\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\indexes\base.py", line 3082, in get_loc
raise KeyError(key) from err
KeyError: 13
my goal is to find the rate of change of the avg glucose
Solution
(forgot to post my solution, hence late update) This is what I did to find the difference of days in order to calculate the rate of change:
#finding the difference in days from each day of glucose monitoring
df1['new'] = df1['Days of Culture'].diff()
# creating a new column which trackes the daily loss in glucose
df1['Daily change in Glucose (mg/dL)'] = df1['Rate'] / df1['new']
Answered By - abrar mahi
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.