Files
python-bootcamp/View_h5_file.py
T

75 lines
2.2 KiB
Python
Raw Normal View History

2023-05-16 16:44:15 -04:00
import h5py
import pandas as pd
2023-05-17 15:01:04 -04:00
import numpy as np
import plotly.graph_objects as go
2023-05-16 16:44:15 -04:00
f = h5py.File('product1.h5','r')
list(f.keys())
for key in f.keys():
print(f[key])
df = pd.read_hdf('product1.h5', key='df')
df = df._append(pd.read_hdf('product1.h5', key))
2023-05-16 16:44:15 -04:00
# print(f[key].shape)
# print(f[key].value)
# df = pd.read_hdf('product1.h5', key='df7')
2023-05-16 16:44:15 -04:00
print(df)
'''
# Open the HDF5 file
with h5py.File('product1.h5', 'r') as file:
dataframes=pd.DataFrame(columns=['Product ID', 'Zip Code', 'Shown Price', 'Minimum Quantity', 'Total Price', 'Unit Price','Sales Count','Sold Status'])
for key in file.keys():
# Read the dataframe from the HDF5 file
dataframe = pd.read_hdf('product1.h5', key)
2023-05-17 15:01:04 -04:00
# Append the dataframe to the list
dataframes._append(dataframe)
# Print the number of dataframes read from the HDF5 file
print(f"Read {len(dataframes)} dataframes from the HDF5 file")
item_std_devs = {}
for dataframe in dataframes:
item_name = dataframe['Product ID'].iloc[0]
std_dev = np.std(dataframe['Unit Price'])
item_std_devs[item_name] = std_dev
# Print the item names and their corresponding standard deviations
for item_name, std_dev in item_std_devs.items():
print(f"Item: {item_name}, Std Dev: {std_dev}")
# Prepare the data for the graph
x = list(item_std_devs.keys())
y = list(item_std_devs.values())
# Create a bar graph
fig = go.Figure(data=[go.Bar(x=x, y=y)])
# Customize the graph
fig.update_layout(
title="Standard Deviation of Pricing for Each Item",
xaxis_title="Item",
yaxis_title="Standard Deviation",
)
# Save the graph as an HTML file
report_file = 'test2.html'
fig.write_html(report_file)
2023-05-22 17:04:22 -04:00
'''
'''
2023-05-17 15:01:04 -04:00
# Calculate the standard deviation using pandas' function
std_dev_unit = df.groupby('Product ID')['Unit Price'].std()
2023-05-17 15:01:04 -04:00
std_dev_displayed = df.groupby('Zip Code')['Shown Price'].std()
# Generate the graph using Plotly
fig = go.Figure()
fig.add_trace(go.Bar(x=std_dev_unit.index, y=std_dev_unit.values))
fig.update_layout(title='Standard Deviation of the Unit Pricing by Item', xaxis_title='Product ID', yaxis_title='Standard Deviation')
2023-05-17 15:01:04 -04:00
report_file = 'test2.html'
2023-05-17 15:01:04 -04:00
fig.write_html(report_file)
2023-05-22 17:04:22 -04:00
'''