In [ ]:
import numpy as np
import pandas as pd

import matplotlib.pyplot as plt
#from statsmodels.graphics.gofplots import qqplot
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots

from ipywidgets import *
from IPython.display import display, HTML
import plotly.io as pio
In [43]:
from ipywidgets.embed import embed_minimal_html, dependency_state
embed_minimal_html('interactive-eda-beetle-damage-export.html',views=[Tree_filter, cause_filter, state_filter,out] )#, state=dependency_state([Tree_filter, cause_filter, state_filter,out]
In [2]:
%precision 2
%config InlineBackend.figure_format = 'retina'
In [6]:
pd.set_option("mode.chained_assignment", None)

Tabelle 41261-0012

C:\\Users\Kurt\Documents\Notebooks\XGBoost\Rainfall Kerala Flood\Data\41261-0012Schadholzeinschlag_Bundesländer_Einschlagsursache.csv ftp://[email protected]/www/python/41261-0012Schadholzeinschlag_Bundeslander_Einschlagsursache.csvimport requests import io url= "http://vanoproy.be/python/41261-0012Schadholzeinschlag_Bundeslander_Einschlagsursache.csv" # /www s=requests.get(url).content c=pd.read_csv(io.StringIO(s.decode('utf-8')), encoding='ANSI',) c
In [62]:
def Schadholzeinschlag_Bundesländer_Einschlagsursache():
    States =['Baden-Württemberg','Bayern','Berlin','Brandenburg','Bremen','Hamburg','Hessen','Mecklenburg-Vorpommern','Niedersachsen',
        'Nordrhein-Westfalen', 'Rheinland-Pfalz','Saarland','Sachsen','Sachsen-Anhalt','Schleswig-Holstein','Thüringen']
    Causes =['Insects', "Wind/Storm", "Snow/Ice","Drought",'Total']  #"unknown",
    Tree_types =['Oak and red oak', 'Beech and other deciduous wood','Pine and larch','Spruce, fir,Douglas fir and other coniferous trees','Total']
    
    url= "http://vanoproy.be/python/41261-0012Schadholzeinschlag_Bundeslander_Einschlagsursache.csv" # www/
    #s=requests.get(url).content
    #c=pd.read_csv(io.StringIO(s.decode('utf-8')))
    Timbercut_causeB = pd.read_csv(url, sep=";",skiprows=6, engine="python",encoding="ANSI",skipfooter=4,decimal=",")# , nrows=nrows
    Timbercut_causeB=Timbercut_causeB[Timbercut_causeB.Owner=="Insgesamt"]
    #Timbercut_causeB 

    #Timbercut_causeB["value"] =Timbercut_causeB["value"].astype( "float32") 
    Timbercut_causeB =Timbercut_causeB.replace("-","0")  # fillna( 0)   #
    lossyears= [2011,2012,2013,2014,2015,2016,2017,2018,2019,2020]

    Timbercut_causeB["Year"]= Timbercut_causeB.loc[:,"Year"].astype("int16")
    Timbercut_causeB2018All= Timbercut_causeB[Timbercut_causeB.Year.isin(lossyears )]   #.copy()
    #Timbercut_causeB2018All.sample(10)

    treetypedict= {"Fichte, Tanne, Douglasie und sonstiges Nadelholz":'Spruce, fir,Douglas fir and other coniferous trees', "Kiefer und Lärche":'Pine and larch',
                       "Buche und sonstiges Laubholz":"Beech and other deciduous wood", "Eiche und Roteiche":"Oak and red oak","Insgesamt":"Total", "Insekten":"Insects","Trockenheit":"Drought",
                       "Schnee/Duft":"Snow/Ice","Wind/Sturm":"Wind/Storm","Sonstiges":"Other"}
    Timbercut_causeB2018All["Cause"]= Timbercut_causeB2018All["Cause"].map( treetypedict )
    Timbercut_causeB2018All["Tree_type"] = Timbercut_causeB2018All["Tree_type"].map( treetypedict)
    df =Timbercut_causeB2018All.melt(id_vars=["Year","Cause","Tree_type","Owner"], var_name="German State")
    df['Year'] =pd.to_datetime( df.Year, format='%Y' ) # .astype('datetime64["Y"]')  np.datetime64(1, 'Y')
    df = df.rename( columns={"value": "Value"}, errors="raise" )            # avoid strange behavior
    df.Value =df.Value.astype( "float32")   #is_numeric() 
    df['Cause'] = df.Cause.replace( np.nan, "unknown") 
    print(df.head() )
    return df
In [63]:
df = Schadholzeinschlag_Bundesländer_Einschlagsursache()
        Year       Cause                                          Tree_type  \
0 2011-01-01  Wind/Storm                                    Oak and red oak   
1 2011-01-01  Wind/Storm                     Beech and other deciduous wood   
2 2011-01-01  Wind/Storm                                     Pine and larch   
3 2011-01-01  Wind/Storm  Spruce, fir,Douglas fir and other coniferous t...   
4 2011-01-01  Wind/Storm                                              Total   

       Owner       German State       Value  
0  Insgesamt  Baden-Württemberg    3.300000  
1  Insgesamt  Baden-Württemberg   28.100000  
2  Insgesamt  Baden-Württemberg    5.800000  
3  Insgesamt  Baden-Württemberg  113.000000  
4  Insgesamt  Baden-Württemberg  150.100006  
In [64]:
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 4800 entries, 0 to 4799
Data columns (total 6 columns):
 #   Column        Non-Null Count  Dtype         
---  ------        --------------  -----         
 0   Year          4800 non-null   datetime64[ns]
 1   Cause         4800 non-null   object        
 2   Tree_type     4800 non-null   object        
 3   Owner         4800 non-null   object        
 4   German State  4800 non-null   object        
 5   Value         4800 non-null   float32       
dtypes: datetime64[ns](1), float32(1), object(4)
memory usage: 206.4+ KB
df.sample(33)#@st.cache def load_data(): # df['date'] = df.Year .astype('datetime64["Y"]') data= df.drop(columns="Owner", axis=1) return data
In [27]:
States =['Baden-Württemberg','Bayern','Berlin','Brandenburg','Bremen','Hamburg','Hessen','Mecklenburg-Vorpommern','Niedersachsen',
        'Nordrhein-Westfalen', 'Rheinland-Pfalz','Saarland','Sachsen','Sachsen-Anhalt','Schleswig-Holstein','Thüringen']
Causes =['Insects', "Wind/Storm", "Snow/Ice","Drought",'Total']  #"unknown",
Tree_types =['Oak and red oak', 'Beech and other deciduous wood','Pine and larch','Spruce, fir,Douglas fir and other coniferous trees','Total']

# Pick State / damage / Tree_type
state_filter = widgets.Select(description='Pick State', options=States, value="Nordrhein-Westfalen",style=dict(font_style='italic', font_weight='bold'),rows=10)
state_filter.description_width=500
cause_filter = widgets.Select(description='Pick cause of damage', options=Causes, value="Insects", description_tooltip='Pick a cause of tree damage', )
cause_filter.description_width=500
Tree_filter = widgets.Select(description='Pick Tree type', options= Tree_types,description_width=500, value="Spruce, fir,Douglas fir and other coniferous trees" ,style=dict(
    font_style='italic', font_weight='bold'))
Tree_filter.description_width=500
#out2 = widgets.Output()
state_filter.style.keysstate_filter.keys

The volume of damaged tree cuttings by federal German state since 2015

The cause of damage are the totals of cuttings in 1000 m³.

def f(a, b, c): print('{}*{}*{}={}'.format(a, b, c, a*b*c)) out = widgets.interactive_output(f, {'a': a, 'b': b, 'c': c}) widgets.HBox([widgets.VBox([a, b, c]), out])data= df.drop(columns="Owner", axis=1) datadata[ (data.loc[:,"Tree_type"]== Tree_filter.value)].head() # .loc
In [29]:
pd.set_option("mode.chained_assignment", None)
In [30]:
def f(a, b, c):
    print('{} & {} & {}'.format(a, b, c))  # 
    
    data= df.copy().drop(columns="Owner", axis=1)
    dfB = data[ (data.loc[:,"Tree_type"]== Tree_filter.value)& (data.loc[:,"Cause"]==cause_filter.value )& (data.loc[:,"German State"]== state_filter.value )]
    dfB = dfB.loc[:, ["Year","Value"]]
    dfB = dfB.set_index("Year")
    #fig, ax = plt.subplots(1,1, )
    dfB.plot(y="Value", figsize=(9,5 ),title=(str(Tree_filter.value)+" in "+str(state_filter.value)),ylabel="1000 m³", grid=True,fontsize=12) # 
    return dfB
In [31]:
out = widgets.interactive_output(f, {'a': Tree_filter, 'b': cause_filter, 'c': state_filter}) # 'd':out2
In [32]:
widgets.HBox([widgets.VBox([ Tree_filter, cause_filter, state_filter ]), out ], width=1600) # display( out2)
In [33]:
Tree_filter.value
Out[33]:
'Spruce, fir,Douglas fir and other coniferous trees'
display(out)

Timelines of total timber cuttings per German state.

In [36]:
#fig, ax = plt.subplots(1,1, )
data=df.copy()
df2 =data[ (data["Tree_type"]== "Total")& (data["Cause"]== "Total" )]  # ["German State","Value"]
df2 =df2 [ (df2["German State"]!= "Berlin")& (df2["German State"]!= "Hamburg" )] 
df2.Value =df2.Value.astype( "float32") 
df2 =df2.loc[:, ["Year","German State","Value"]]
#df2 = df2.set_index(["Year","German State"]) # 
#df2 = df2.sort_index() 
fig= px.line( df2,x="Year",y="Value",color="German State",log_y=True,markers=True,symbol="German State",template="seaborn",)  #linewidth=1 barmode="group",
fig.update_layout(title='Total timber cuttings per German state', #xaxis_title='Month',
                   yaxis_title='$1000 m³$)')
fig.show()