DATA VISUALIZATION WITH PYTHON

            DATA VISUALIZATION IN PYTHON USING STREAMLIT


Have you ever thought of visualizing Data by reading it from the excel sheet using OOP's concept and it's various techniques .
One of the part i know is that reading Data from the excel sheet and making the histogram and chart.
The below Data visualization is done by the help of python programming Language using streamlit module.

import streamlit as st
import pandas as pd
import numpy as np //for the analytics
import pydeck as pdk //library for Data Vizualization
import plotly.express as px //library for histogram

DATA_URL=("D:/project py web app/Motor_Vehicle_Collisions_-_Crashes.csv")
st.title("MOTOR vehicle Motor_Vehicle_Collisions_")
st.markdown("This application is a streamlit dashboard that can be used "
"to analyze motor vehicle collision🗽🚗")
@st.cache(persist=True)
def load_data(nrows):
    data=pd.read_csv(DATA_URL,nrows=nrows,parse_dates=[['CRASH_DATE','CRASH_TIME']])
    data.dropna(subset=['LATITUDE','LONGITUDE'],inplace=True)
    lowercase=lambda x:str(x).lower()
    data.rename(lowercase, axis='columns',inplace=True)
    data.rename(columns={'crash_date_crash_time':'date/time'},inplace=True)
    return data

data =load_data(100000)
original_data=data
# data visualization
st.header("Where are the most people injured in NYC?")
injured_people=st.slider("Number of persons injured in Vehicle Collisions ",0,19)
st.map(data.query("injured_persons>=@injured_people")[["latitude","longitude"]].dropna(how="any"))
st.header("How many Collisions occur during a given time of day?")
hour=st.slider("hour to look at",0,23)
data=data[data['date/time'].dt.hour == hour]

#ploting data on 3d map

st.markdown("vehiclecollisions between %i:00 and %i:00" %(hour,(hour+1)%24))
midpoint = (np.average(data['latitude']),np.average(data['longitude']))
st.write(pdk.Deck(
    map_style="mapbox://styles/mapbox/light-v9",
    initial_view_state={
        "latitude":midpoint[0],
        "longitude":midpoint[1],
        "zoom":11,
        "pitch":50,

    },
    #3d layer
    layers=[
    pdk.Layer(
    "HexagonLayer",
    data=data[['date/time','latitude','longitude']],
    get_position=['longitude','latitude'],
    radius=100,
    extruded=True,
    pickable=True,
    elevation_scale=4,
    eleveation_range=[0,1000],

    ),
    ],


))

st.subheader("Breakdown by minute between %i:00 and %i:00" %(hour,(hour+1)%24))
filtered =data[
(data['date/time'].dt.hour>= hour)&(data['date/time'].dt.hour<(hour+1))
]
#HISTAGRAM
hist=np.histogram(filtered['date/time'].dt.minute, bins=60, range=(0,60))[0]
chart_data = pd.DataFrame({'minute':range(60),'crashes':hist})
fig=px.bar(chart_data,x='minute',y='crashes',hover_data=['minute','crashes'],height=400)
st.write(fig)

st.header("Top 5 dangerous streets by affected type")
select =st.selectbox('Affexted type of people',['Pedestrians','cyclists','Motorists'])
if select == 'Pedestrians':
    st.write(original_data.query("injured_pedestrians>=1")[["on_street_name","injured_pedestrians"]].sort_values(by=['injured_pedestrians'],ascending=False).dropna(how='any')[:5])
elif select == 'Motorists':
    st.write(original_data.query("injured_Motorists >=1")[["on_street_name","injured_Motorists"]].sort_values(by=['injured_Motorists'],ascending=False).dropna(how='any')[:5])
elif select == 'cyclists':
    st.write(original_data.query("injured_cyclists >=1")[["on_street_name","injured_cyclists"]].sort_values(by=['injured_cyclists'],ascending=False).dropna(how='any')[:5])


if st.checkbox("Show Raw Data",False):
    st.subheader('Raw Data')
    st.write(data)

The excel file from which the data is read is listed in the csv file given below.

OUTPUT AFTER WRITING THE CODE IS :-

open cmd in the file where you write and save app.py file.











now run the following command in cmd.

To launch the streamlite Data visualization app which is built by you



The final output after runnig the app the output we get is






                                                                 Written by:- RITIK

Comments

  1. This comment has been removed by the author.

    ReplyDelete
  2. Hi your blog is cool. I will share with my friends too. My colleagues were taking about cloudcouncil that they provide certification on Python. As you have knowledge in it can you kindly help me is that worth studying and get certified ? https://cloudcouncil.org/

    ReplyDelete

Post a Comment