Spaces:
Sleeping
Sleeping
Commit
·
6e9e750
1
Parent(s):
4401c98
Add Likert data vizualisations
Browse files
app.py
CHANGED
@@ -1,4 +1,159 @@
|
|
1 |
import streamlit as st
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import matplotlib.pyplot as plt
|
4 |
+
import seaborn as sns
|
5 |
|
6 |
+
# Streamlit title and introduction
|
7 |
+
st.title("Likert Scale Visualizations")
|
8 |
+
st.write("This app visualizes responses to a survey.")
|
9 |
+
|
10 |
+
# Upload the CSV file
|
11 |
+
uploaded_file = st.file_uploader("Choose a CSV file", type="csv")
|
12 |
+
|
13 |
+
if uploaded_file is not None:
|
14 |
+
# Load the CSV file into a DataFrame
|
15 |
+
df = pd.read_csv(uploaded_file)
|
16 |
+
|
17 |
+
likert_fields = {
|
18 |
+
'購物習慣': [
|
19 |
+
'如果你/妳懷疑你/妳要買的番茄可能是由強迫勞工(現代奴隸)採摘的,你/妳仍然會買它嗎?',
|
20 |
+
'你/妳關心食安嗎?',
|
21 |
+
'你/妳7年內買車嗎?🚘',
|
22 |
+
'你/妳7年內買房嗎?🏡',
|
23 |
+
'你/妳購物時知道產品環保嗎?',
|
24 |
+
'你/妳覺得認證環保的公司更好嗎?',
|
25 |
+
'你/妳支持肉稅嗎?',
|
26 |
+
'你/妳關心食用雞的生活嗎?',
|
27 |
+
'你/妳避免吃肉嗎?',
|
28 |
+
'你/妳覺得你/妳花錢會影響環境嗎?'
|
29 |
+
],
|
30 |
+
'存錢和投資習慣': [
|
31 |
+
'你/妳會對金錢感到焦慮嗎?',
|
32 |
+
'你/妳會對金錢很節儉嗎?',
|
33 |
+
'你/妳會經常存錢嗎?',
|
34 |
+
'你/妳對自己的財務知識滿意嗎?',
|
35 |
+
'你/妳投資會考慮環保嗎?'
|
36 |
+
],
|
37 |
+
'台灣的經濟': [
|
38 |
+
'你/妳覺得台灣的經濟目標是增長嗎?',
|
39 |
+
'你/妳覺台灣的得環境退化是台灣的經濟增長的前提嗎?',
|
40 |
+
'你/妳覺得台灣的經濟增長有助於保護環境嗎?',
|
41 |
+
'你/妳覺得經濟能不排CO2也增長嗎?',
|
42 |
+
'你/妳覺得經濟增長有物質限制嗎?'
|
43 |
+
],
|
44 |
+
'AI使用習慣': [
|
45 |
+
'你/妳會每天都用AI嗎?',
|
46 |
+
'你/妳會信任AI嗎?',
|
47 |
+
'你/妳想要AI有個造型嗎?',
|
48 |
+
],
|
49 |
+
'台灣的大自然': [
|
50 |
+
'你/妳喜歡待在大自然嗎?',
|
51 |
+
'你/妳擔心氣候變化嗎?',
|
52 |
+
'你/妳對環境污染情況會感到焦慮嗎?',
|
53 |
+
'你/妳知道許多植物和動物的名字嗎?',
|
54 |
+
'你/妳感覺自己和大自然很接近嗎?'
|
55 |
+
],
|
56 |
+
'環保習慣': [
|
57 |
+
'你/妳努力實踐低碳生活嗎?',
|
58 |
+
'你/妳想做更多環保事嗎?',
|
59 |
+
'你/妳對環境相關政治議題有興趣嗎?',
|
60 |
+
'你/妳信任碳排放抵消額度嗎?',
|
61 |
+
'你/妳的環保行動對環境保護有效果嗎?',
|
62 |
+
'你/妳想在行業內推環保嗎?'
|
63 |
+
],
|
64 |
+
'學習環境': [
|
65 |
+
'你/妳得自己對新觀念開放嗎?',
|
66 |
+
'你/妳的大學對可環保性支持嗎?'
|
67 |
+
]
|
68 |
+
}
|
69 |
+
|
70 |
+
# Translation mapping for the categories
|
71 |
+
translation_mapping = {
|
72 |
+
'購物習慣': 'Shopping Habits',
|
73 |
+
'存錢和投資習慣': 'Saving and Investing Habits',
|
74 |
+
'台灣的經濟': "Taiwan's Economy",
|
75 |
+
'AI使用習慣': 'AI Usage Habits',
|
76 |
+
'台灣的大自然': "Taiwan's Natural Environment",
|
77 |
+
'環保習慣': 'Environmental Habits',
|
78 |
+
'學習環境': 'Learning Environment'
|
79 |
+
}
|
80 |
+
|
81 |
+
# Translation mapping for the field names in each category (this should match your actual field names)
|
82 |
+
field_translation_mapping = {
|
83 |
+
'購物習慣': [
|
84 |
+
"Would you still buy a tomato if you suspected it was picked by forced labor?",
|
85 |
+
"Do you care about food safety?",
|
86 |
+
"Will you buy a car within 7 years?",
|
87 |
+
"Will you buy a house within 7 years?",
|
88 |
+
"Do you know if the product is environmentally friendly when shopping?",
|
89 |
+
"Do you think companies with environmental certifications are better?",
|
90 |
+
"Do you support a meat tax?",
|
91 |
+
"Do you care about the living conditions of chickens you consume?",
|
92 |
+
"Do you avoid eating meat?",
|
93 |
+
"Do you think your spending affects the environment?"
|
94 |
+
],
|
95 |
+
'存錢和投資習慣': [
|
96 |
+
"Do you feel anxious about money?",
|
97 |
+
"Are you frugal with money?",
|
98 |
+
"Do you save money regularly?",
|
99 |
+
"Are you satisfied with your financial knowledge?",
|
100 |
+
"Do you consider environmental impact when investing?"
|
101 |
+
],
|
102 |
+
'台灣的經濟': [
|
103 |
+
"Do you think the economic goal of Taiwan is growth?",
|
104 |
+
"Do you think that environmental degradation is a prerequisite for Taiwan's economic growth?",
|
105 |
+
"Do you think Taiwan's economic growth contributes to environmental protection?",
|
106 |
+
"Do you think the economy can grow without emitting CO2?",
|
107 |
+
"Do you think economic growth has material limits?"
|
108 |
+
],
|
109 |
+
'AI使用習慣': [
|
110 |
+
"Do you use AI every day?",
|
111 |
+
"Do you trust AI?",
|
112 |
+
"Would you like AI to have a form?"
|
113 |
+
],
|
114 |
+
'台灣的大自然': [
|
115 |
+
"Do you like to be in nature?",
|
116 |
+
"Are you worried about climate change?",
|
117 |
+
"Do you feel anxious about environmental pollution?",
|
118 |
+
"Do you know the names of many plants and animals?",
|
119 |
+
"Do you feel close to nature?"
|
120 |
+
],
|
121 |
+
'環保習慣': [
|
122 |
+
"Do you strive to live a low-carbon life?",
|
123 |
+
"Would you like to do more for the environment?",
|
124 |
+
"Are you interested in environmental political issues?",
|
125 |
+
"Do you trust carbon offset credits?",
|
126 |
+
"Do you think your environmental actions are effective for environmental protection?",
|
127 |
+
"Would you like to promote environmental protection in your industry?"
|
128 |
+
],
|
129 |
+
'學習環境': [
|
130 |
+
"Are you open to new ideas?",
|
131 |
+
"Does your university support sustainability?"
|
132 |
+
]
|
133 |
+
}
|
134 |
+
|
135 |
+
# Rename the columns in the DataFrame for visualization
|
136 |
+
df_translated = df.rename(columns={
|
137 |
+
field: f"{field} ({field_translation_mapping[category][i]})"
|
138 |
+
for category, fields in likert_fields.items()
|
139 |
+
for i, field in enumerate(fields)
|
140 |
+
})
|
141 |
+
|
142 |
+
# Loop through each category in likert_fields to create visualizations
|
143 |
+
for category, fields in likert_fields.items():
|
144 |
+
st.subheader(f'Distribution of Responses for {translation_mapping[category]}')
|
145 |
+
|
146 |
+
# Loop through each field in the category to create individual bar plots
|
147 |
+
for i, field in enumerate(fields):
|
148 |
+
fig, ax = plt.subplots()
|
149 |
+
|
150 |
+
# Create the bar plot
|
151 |
+
sns.countplot(x=f"{field} ({field_translation_mapping[category][i]})", data=df_translated, ax=ax)
|
152 |
+
|
153 |
+
# Add title and labels
|
154 |
+
ax.set_title(field_translation_mapping[category][i])
|
155 |
+
ax.set_xlabel('Likert Scale')
|
156 |
+
ax.set_ylabel('Frequency')
|
157 |
+
|
158 |
+
# Show the plot in Streamlit
|
159 |
+
st.pyplot(fig)
|