Forest数据分析及可视化
文章目录
-
* Forest数据分析及可视化
-
* 说在前面
- 1.导入csv数据集
- 2.分析种树种类
- 3.分析种树标签
- 4.种植树木的成功率
- 5.基于周和月的种树时间分析
- 6.总结
- 附源代码
-
说在前面
-
这次python作业是数据分析及可视化,这里我采用的是一个APP “Forest” ,从软件中导出 “Plants of forest.csv”,这是csv格式的数据集
-
Forest是一个让你规划时间,放下手机保持专注的APP,当需要专注的时候就种下一棵树并倒计时,时间结束,树就会长大,如果中途离开Forest,树就会枯死,最终所有种树记录都会保存,并且可以导出csv文件。
-
这里推荐一下Forest的官网,有需要的小伙伴可以去下载试试,可以提高学习效率。 https://www.forestapp.cc/
下面是种树后的截图:(来源官网)
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200717101206984.png?x-oss-
process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwMTgxNTky,size_16,color_FFFFFF,t_70#pic_center)
1.导入csv数据集
- 下面是我截取的部分csv数据集:
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200717101220334.png?x-oss-
process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwMTgxNTky,size_16,color_FFFFFF,t_70#pic_center)
-
首先可以看出这里有StartTime(开始种树的时间)、EndTime(结束种树的时间)、Tag(标签,里面有"学习"、“休息”、"睡觉"等)、Note(自己设置的备注)、TreeType(每次种树的种类)、IsSuccess(是否种树成功)。
-
下面首先将该csv文件读进来,使用pandas中的read_csv()方法将其读进来保存到data中,之后可以用data[‘Tag’]取出标签这一列,其余列也是类似,这样就可以导入csv文件数据了。
[code] data = pd.read_csv(‘Plants of forest.csv’)tag = data[‘Tag’]
### 2.分析种树种类
* 先对种树的种类进行分析:用 ` list(data[‘TreeType’]) ` 取出TreeType这一列数据并转化为列表,这里定义一个字典用来统计不同种类树的个数,遍历列表中每一个数据,使用count()方法统计个数并和该类型的种类组成一个键值对存到字典中,之后用sum(tree_dic.values)对种下的数目总数进行统计,并用sort方法对字典中树木类型的数量进行排序并打印输出。
[code] def tree_type_analyse(data):
"""分析种树种类"""
tree_type = list(data['Tree Type'])
tree_dic = {}
for i in tree_type:
tree_dic[i] = tree_type.count(i)
tree_list = list(tree_dic.items())
tree_list.sort(key= lambda x:x[1], reverse=True)
tree_type_count = len(tree_list)
tree_count = sum(tree_dic.values())
sorted_tree = []
sorted_count = []
print('you have planted {0} trees, There are {1} types of trees'.format(tree_count, tree_type_count))
print ('\tTree Type \t\t\t counts')
print ('=================================')
for i in range(0, len(tree_list)):
tree, count = tree_list[i]
if i <= 9:
sorted_tree.append(tree)
sorted_count.append(count)
print("{0:<30}{1:<5}".format(tree, count))
下面是打印输出结果:
[code] you have planted 433 trees, There are 40 types of trees
Tree Type counts
=================================
Moon Tree