I'm relatively new to working with pandas dataframes to read in data and I'm having some trouble working with my dataset. I've been reading many other StackOverflow posts on a similar problem but I've been having trouble applying those solutions to my case possibly because of the structure of my JSON data. My JSON data as arranged into my dataframe df = pd.DataFrame.from_records(data)
generally looks like this
dateTime value
0 01/16/20 04:32:42 {'bpm': 70, 'confidence': 0}
1 01/16/20 04:32:57 {'bpm': 70, 'confidence': 0}
2 01/16/20 04:33:12 {'bpm': 70, 'confidence': 1}
My goal is to read all of this raw daily data and compute a monthly average of "bpm" and plot to a matplot graph. My problem is I'm having trouble using pandas datetime or mean() operations because I don't think pandas accepts my dateTime format as actually in datetime and when I try to use mean() it gives me an pandas.core.base.DataError: No numeric types to aggregate
error.
How can I use built in pandas tools to allow me to compute a monthly average by grouping my daily values together based on the month?
for file in os.listdir(data_dir): # look at every file in the folder
if file.startswith("heart_rate") and file.endswith(".json"): # only want heart_rate-date.json files
with open(os.path.join(data_dir, file)) as f: # open each file in data_dir
data = json.load(f)
df = pd.DataFrame.from_records(data)
print(df)
#df.dateTime = pd.to_datetime(df.dateTime)
#df['Month'] = df['dateTime'].dt.month
for i, j in enumerate(data):
if data[i]['value']['confidence'] > 0:
daily_avg_bpm += data[i]['value']['bpm']
daily_date = data[i]['dateTime'].split()[0]
my_date = datetime.datetime.strptime(daily_date, "%m/%d/%y").date()
days.append(my_date)
months.append(daily_date[:2])
daily_avg_bpm /= len(data)
dates.append(daily_date)
avg_bpms.append(round(daily_avg_bpm))
f.close()
plt.xlabel('Month')
plt.ylabel('Heart Rate')
plt.title("Fitbit Heart Rate")
for i, j in enumerate(dates):
plt.plot(dates[i], avg_bpms[i])
plt.show()
resample
solve your issue? – Prayson W. Danieldf.dtypes
? – Prayson W. Danieldf['value'].apply(pd.Series)
? Does that expand value? – Prayson W. Daniel