Q010 - Che dotazioni aggiuntive ha la sede che ospita il laboratorio?


In [1]:
# -*- coding: UTF-8 -*-

# Render our plots inline
%matplotlib inline 

import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import seaborn
import shutil

pd.set_option('display.mpl_style', 'default') # Make the graphs a bit prettier, overridden by seaborn
pd.set_option('display.max_columns', None) # Display all the columns
plt.rcParams['font.family'] = 'sans-serif' # Sans Serif fonts for all the graphs

# Reference for color palettes: http://web.stanford.edu/~mwaskom/software/seaborn/tutorial/color_palettes.html

# Change the font
matplotlib.rcParams.update({'font.family': 'Source Sans Pro'})

In [2]:
# Load csv file first
data = pd.read_csv("data/lab-survey.csv", encoding="utf-8")

In [3]:
# Check data
#data[0:4] # Equals to data.head()

In [4]:
# Range: D10[SQ001] - D10[SQ008] - D10[other]

equipment_columns = ["D10[SQ001]","D10[SQ002]","D10[SQ003]","D10[SQ004]",
               "D10[SQ005]","D10[SQ006]","D10[SQ007]","D10[SQ008]"]
equipment_options = ['Cucina',
               'Bar',
               'Auditorium',
               'Sale lezioni',
               'Altri equipmentoratori',
               'Magazzino del equipmentoratorio',
               'Magazzino per gli utenti',
               'Scrivanie o postazioni di lavoro']
equipment = data[equipment_columns]
equipment.replace(u'Sì', 'Si', inplace=True) # Get rid of accented characters 
equipment_other = data['D10[other]'].str.lower().value_counts()


-c:14: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy

In [5]:
#lab[0:4]

In [6]:
%%capture output

# Save the output as a variable that can be saved to a file
# Gather data
equipment_b = {}
for k,i in enumerate(equipment_columns):
    equipment_b[k] = equipment[i].value_counts(dropna=False)
    print "Data:",equipment_options[k]
    print equipment_b[k]
    print
    print "Data %:",equipment_options[k]
    print equipment[i].value_counts(normalize=True,dropna=False)*100
    print

In [7]:
# Save+show the output to a text file
%save Q010-DotazioniLab01.py str(output)
shutil.move("Q010-DotazioniLab01.py", "text/Q010-DotazioniLab01.txt")


The following commands were written to file `Q010-DotazioniLab01.py`:
Data: Cucina
No     51
Si     19
NaN     0
dtype: int64

Data %: Cucina
No     72.857143
Si     27.142857
NaN     0.000000
dtype: float64

Data: Bar
No     52
Si     18
NaN     0
dtype: int64

Data %: Bar
No     74.285714
Si     25.714286
NaN     0.000000
dtype: float64

Data: Auditorium
No     48
Si     22
NaN     0
dtype: int64

Data %: Auditorium
No     68.571429
Si     31.428571
NaN     0.000000
dtype: float64

Data: Sale lezioni
Si     42
No     28
NaN     0
dtype: int64

Data %: Sale lezioni
Si     60
No     40
NaN     0
dtype: float64

Data: Altri equipmentoratori
No     38
Si     32
NaN     0
dtype: int64

Data %: Altri equipmentoratori
No     54.285714
Si     45.714286
NaN     0.000000
dtype: float64

Data: Magazzino del equipmentoratorio
Si     36
No     34
NaN     0
dtype: int64

Data %: Magazzino del equipmentoratorio
Si     51.428571
No     48.571429
NaN     0.000000
dtype: float64

Data: Magazzino per gli utenti
No     60
Si     10
NaN     0
dtype: int64

Data %: Magazzino per gli utenti
No     85.714286
Si     14.285714
NaN     0.000000
dtype: float64

Data: Scrivanie o postazioni di lavoro
Si     53
No     17
NaN     0
dtype: int64

Data %: Scrivanie o postazioni di lavoro
Si     75.714286
No     24.285714
NaN     0.000000
dtype: float64



In [8]:
yes = []
no = []
nanvalue = []

# Get data for plotting, and check that we have all the values for Yes and No
for k,i in enumerate(equipment_columns):
    equipment_presents = equipment_b[k].index.tolist()
    
    # Convert NaN to "NaN"
    for o,h in enumerate(equipment_presents):
        if type(h) is float:
            equipment_presents.pop(o)
            equipment_presents.append("NaN")
    
    # Reassign new list with "NaN"
    equipment_b[k].index = equipment_presents
    
    # Check for empty values, and put a 0 instead
    if "Si" not in equipment_presents:
        yes.append(0)
    if "No" not in equipment_presents:
        no.append(0)
    if "NaN" not in equipment_presents:
        nanvalue.append(0)
    
    for j in equipment_presents:
        if j == "Si":
            yes.append(equipment_b[k].ix["Si"])
        elif j == "No":
            no.append(equipment_b[k].ix["No"])
        elif j == "NaN":
            nanvalue.append(equipment_b[k].ix["NaN"])

In [9]:
# Plot the data
plt.figure(figsize=(8,6))
plt.xlabel(u'Dotazioni', fontsize=16)
plt.ylabel(u'Lab', fontsize=16)
plt.title(u'Che dotazioni aggiuntive ha la sede che ospita il laboratorio?', fontsize=18, y=1.02)
plt.xticks(range(len(equipment_options)),equipment_options,rotation=90)
ind = np.arange(len(equipment_columns))   # the x locations for the groups
width = 0.25                              # the width of the bars

my_colors = seaborn.color_palette("Set1", 3) # Set color palette
rect1 = plt.bar(ind,yes,width,color=my_colors[1],align='center') # Plot Yes
rect2 = plt.bar(ind+width,no,width,color=my_colors[0],align='center') # Plot No 
rect3 = plt.bar(ind+width*2,nanvalue,width,color=my_colors[2],align='center') # Plot NaN
plt.legend( (rect1, rect2, rect3), ('Si', 'No', 'Nessuna risposta') )
plt.savefig("svg/Q010-DotazioniLab01.svg")
plt.savefig("png/Q010-DotazioniLab01.png")
plt.savefig("pdf/Q010-DotazioniLab01.pdf")



In [10]:
%%capture output

# Save the output as a variable that can be saved to a file
# Get "other" data
equipment_other = data["D10[other]"].str.lower().value_counts()
print "Data:"
print equipment_other
print ""
print "Data %:"
print data["D10[other]"].str.lower().value_counts(normalize=True) * 100

In [11]:
# Save+show the output to a text file
%save Q010-DotazioniLab02.py str(output)
shutil.move("Q010-DotazioniLab02.py", "text/Q010-DotazioniLab02.txt")


The following commands were written to file `Q010-DotazioniLab02.py`:
Data:
sala concerti, sale prove                             1
teatro                                                1
area relax                                            1
spazio esterno 300 mq                                 1
teatro di posa                                        1
parco condominiale                                    1
sala riunioni                                         1
forni per ceramica                                    1
commistione tra spazi fab lab e spazi di coworking    1
laboratorio didattici                                 1
open space da strutturare                             1
residenza per makers (due stanze)                     1
dtype: int64

Data %:
sala concerti, sale prove                             1.428571
teatro                                                1.428571
area relax                                            1.428571
spazio esterno 300 mq                                 1.428571
teatro di posa                                        1.428571
parco condominiale                                    1.428571
sala riunioni                                         1.428571
forni per ceramica                                    1.428571
commistione tra spazi fab lab e spazi di coworking    1.428571
laboratorio didattici                                 1.428571
open space da strutturare                             1.428571
residenza per makers (due stanze)                     1.428571
dtype: float64


In [12]:
# Plot bar
plt.figure(figsize=(8,6))
plt.title(u'Che dotazioni aggiuntive ha la sede che ospita il laboratorio? Altro', fontsize=18, y=1.02)
plt.xticks(range(len(equipment_other.index)),equipment_other.index,rotation=90)
plt.xlabel('Dotazioni', fontsize=16)
plt.ylabel('Lab', fontsize=16)
ind = np.arange(len(equipment_other))   # the x locations for the groups
width = 0.35                       # the width of the bars

my_colors = seaborn.color_palette("husl", len(equipment_other)) # Set color palette
rect1 = plt.bar(ind,equipment_other,width,color=my_colors,align='center')
plt.savefig("svg/Q010-DotazioniLab02.svg")
plt.savefig("png/Q010-DotazioniLab02.png")
plt.savefig("pdf/Q010-DotazioniLab02.pdf")