【Python + ArcGIS】数据分析实战
——学生房前屋后的物种多样性调查
0 前言
源数据经清洗后仅110行数据,数据量较小。但由于问卷设计和填写的不专业,数据类型和内容没有保障,导致数据结构复杂,对于新手和经常需要代做数据分析的人们来说可以作为很好的案例。
目录
【Python + ArcGIS】数据分析实战——学生房前屋后的物种多样性调查
0 前言1 数据格式与分析需求1.1 数据格式1.2 分析需求
2 工具准备3 数据处理流程4 数据处理4.1 初步处理4.2 Python处理4.2.1 对于 需求1 的 Python 处理4.2.2 对于 需求1 的地理处理4.2.3 对于 需求2 的 Python 处理
4 分析结果·需求达成
1 数据格式与分析需求
1.1 数据格式
以下是本人收到的数据Excel文件的表头与样例数据,具体数据来源应该为面向学生发放的调查问卷。数据包括了:
问卷的一些基本信息
当前时间、当前位置 一个看上去像List但是比较难自动化处理的复杂的数据
1-10号植物信息 照片:网址(字符串),名称:字符串,分类:字符串
学生感想与自我评分。 学生感想:字符串,自我评分:数字
1.2 分析需求
以物种为单位,选择5种频数最高的植物物种制作植物的分布地图,从而分析某个植物的地域分异规律,从生长地的生物和地理的角度解释物种分布的合理性。
汇总各个行政区的物种,从物种数量上进行物种多样性比较,概括植物的种类特征以及差异的原因。
2 工具准备
Microsoft Office Excel
Python 涉及的库:pandas
ArcGIS
3 数据处理流程
#mermaid-svg-P4Kyuz2sYc2Wfoeq .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-P4Kyuz2sYc2Wfoeq .label text{fill:#333}#mermaid-svg-P4Kyuz2sYc2Wfoeq .node rect,#mermaid-svg-P4Kyuz2sYc2Wfoeq .node circle,#mermaid-svg-P4Kyuz2sYc2Wfoeq .node ellipse,#mermaid-svg-P4Kyuz2sYc2Wfoeq .node polygon,#mermaid-svg-P4Kyuz2sYc2Wfoeq .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-P4Kyuz2sYc2Wfoeq .node .label{text-align:center;fill:#333}#mermaid-svg-P4Kyuz2sYc2Wfoeq .node.clickable{cursor:pointer}#mermaid-svg-P4Kyuz2sYc2Wfoeq .arrowheadPath{fill:#333}#mermaid-svg-P4Kyuz2sYc2Wfoeq .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-P4Kyuz2sYc2Wfoeq .flowchart-link{stroke:#333;fill:none}#mermaid-svg-P4Kyuz2sYc2Wfoeq .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-P4Kyuz2sYc2Wfoeq .edgeLabel rect{opacity:0.9}#mermaid-svg-P4Kyuz2sYc2Wfoeq .edgeLabel span{color:#333}#mermaid-svg-P4Kyuz2sYc2Wfoeq .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-P4Kyuz2sYc2Wfoeq .cluster text{fill:#333}#mermaid-svg-P4Kyuz2sYc2Wfoeq div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-P4Kyuz2sYc2Wfoeq .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-P4Kyuz2sYc2Wfoeq text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-P4Kyuz2sYc2Wfoeq .actor-line{stroke:grey}#mermaid-svg-P4Kyuz2sYc2Wfoeq .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-P4Kyuz2sYc2Wfoeq .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-P4Kyuz2sYc2Wfoeq #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-P4Kyuz2sYc2Wfoeq .sequenceNumber{fill:#fff}#mermaid-svg-P4Kyuz2sYc2Wfoeq #sequencenumber{fill:#333}#mermaid-svg-P4Kyuz2sYc2Wfoeq #crosshead path{fill:#333;stroke:#333}#mermaid-svg-P4Kyuz2sYc2Wfoeq .messageText{fill:#333;stroke:#333}#mermaid-svg-P4Kyuz2sYc2Wfoeq .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-P4Kyuz2sYc2Wfoeq .labelText,#mermaid-svg-P4Kyuz2sYc2Wfoeq .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-P4Kyuz2sYc2Wfoeq .loopText,#mermaid-svg-P4Kyuz2sYc2Wfoeq .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-P4Kyuz2sYc2Wfoeq .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-P4Kyuz2sYc2Wfoeq .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-P4Kyuz2sYc2Wfoeq .noteText,#mermaid-svg-P4Kyuz2sYc2Wfoeq .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-P4Kyuz2sYc2Wfoeq .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-P4Kyuz2sYc2Wfoeq .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-P4Kyuz2sYc2Wfoeq .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-P4Kyuz2sYc2Wfoeq .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-P4Kyuz2sYc2Wfoeq .section{stroke:none;opacity:0.2}#mermaid-svg-P4Kyuz2sYc2Wfoeq .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-P4Kyuz2sYc2Wfoeq .section2{fill:#fff400}#mermaid-svg-P4Kyuz2sYc2Wfoeq .section1,#mermaid-svg-P4Kyuz2sYc2Wfoeq .section3{fill:#fff;opacity:0.2}#mermaid-svg-P4Kyuz2sYc2Wfoeq .sectionTitle0{fill:#333}#mermaid-svg-P4Kyuz2sYc2Wfoeq .sectionTitle1{fill:#333}#mermaid-svg-P4Kyuz2sYc2Wfoeq .sectionTitle2{fill:#333}#mermaid-svg-P4Kyuz2sYc2Wfoeq .sectionTitle3{fill:#333}#mermaid-svg-P4Kyuz2sYc2Wfoeq .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-P4Kyuz2sYc2Wfoeq .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-P4Kyuz2sYc2Wfoeq .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-P4Kyuz2sYc2Wfoeq .grid path{stroke-width:0}#mermaid-svg-P4Kyuz2sYc2Wfoeq .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-P4Kyuz2sYc2Wfoeq .task{stroke-width:2}#mermaid-svg-P4Kyuz2sYc2Wfoeq .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-P4Kyuz2sYc2Wfoeq .taskText:not([font-size]){font-size:11px}#mermaid-svg-P4Kyuz2sYc2Wfoeq .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-P4Kyuz2sYc2Wfoeq .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-P4Kyuz2sYc2Wfoeq .task.clickable{cursor:pointer}#mermaid-svg-P4Kyuz2sYc2Wfoeq .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-P4Kyuz2sYc2Wfoeq .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-P4Kyuz2sYc2Wfoeq .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-P4Kyuz2sYc2Wfoeq .taskText0,#mermaid-svg-P4Kyuz2sYc2Wfoeq .taskText1,#mermaid-svg-P4Kyuz2sYc2Wfoeq .taskText2,#mermaid-svg-P4Kyuz2sYc2Wfoeq .taskText3{fill:#fff}#mermaid-svg-P4Kyuz2sYc2Wfoeq .task0,#mermaid-svg-P4Kyuz2sYc2Wfoeq .task1,#mermaid-svg-P4Kyuz2sYc2Wfoeq .task2,#mermaid-svg-P4Kyuz2sYc2Wfoeq .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-P4Kyuz2sYc2Wfoeq .taskTextOutside0,#mermaid-svg-P4Kyuz2sYc2Wfoeq .taskTextOutside2{fill:#000}#mermaid-svg-P4Kyuz2sYc2Wfoeq .taskTextOutside1,#mermaid-svg-P4Kyuz2sYc2Wfoeq .taskTextOutside3{fill:#000}#mermaid-svg-P4Kyuz2sYc2Wfoeq .active0,#mermaid-svg-P4Kyuz2sYc2Wfoeq .active1,#mermaid-svg-P4Kyuz2sYc2Wfoeq .active2,#mermaid-svg-P4Kyuz2sYc2Wfoeq .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-P4Kyuz2sYc2Wfoeq .activeText0,#mermaid-svg-P4Kyuz2sYc2Wfoeq .activeText1,#mermaid-svg-P4Kyuz2sYc2Wfoeq .activeText2,#mermaid-svg-P4Kyuz2sYc2Wfoeq .activeText3{fill:#000 !important}#mermaid-svg-P4Kyuz2sYc2Wfoeq .done0,#mermaid-svg-P4Kyuz2sYc2Wfoeq .done1,#mermaid-svg-P4Kyuz2sYc2Wfoeq .done2,#mermaid-svg-P4Kyuz2sYc2Wfoeq .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-P4Kyuz2sYc2Wfoeq .doneText0,#mermaid-svg-P4Kyuz2sYc2Wfoeq .doneText1,#mermaid-svg-P4Kyuz2sYc2Wfoeq .doneText2,#mermaid-svg-P4Kyuz2sYc2Wfoeq .doneText3{fill:#000 !important}#mermaid-svg-P4Kyuz2sYc2Wfoeq .crit0,#mermaid-svg-P4Kyuz2sYc2Wfoeq .crit1,#mermaid-svg-P4Kyuz2sYc2Wfoeq .crit2,#mermaid-svg-P4Kyuz2sYc2Wfoeq .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-P4Kyuz2sYc2Wfoeq .activeCrit0,#mermaid-svg-P4Kyuz2sYc2Wfoeq .activeCrit1,#mermaid-svg-P4Kyuz2sYc2Wfoeq .activeCrit2,#mermaid-svg-P4Kyuz2sYc2Wfoeq .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-P4Kyuz2sYc2Wfoeq .doneCrit0,#mermaid-svg-P4Kyuz2sYc2Wfoeq .doneCrit1,#mermaid-svg-P4Kyuz2sYc2Wfoeq .doneCrit2,#mermaid-svg-P4Kyuz2sYc2Wfoeq .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-P4Kyuz2sYc2Wfoeq .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-P4Kyuz2sYc2Wfoeq .milestoneText{font-style:italic}#mermaid-svg-P4Kyuz2sYc2Wfoeq .doneCritText0,#mermaid-svg-P4Kyuz2sYc2Wfoeq .doneCritText1,#mermaid-svg-P4Kyuz2sYc2Wfoeq .doneCritText2,#mermaid-svg-P4Kyuz2sYc2Wfoeq .doneCritText3{fill:#000 !important}#mermaid-svg-P4Kyuz2sYc2Wfoeq .activeCritText0,#mermaid-svg-P4Kyuz2sYc2Wfoeq .activeCritText1,#mermaid-svg-P4Kyuz2sYc2Wfoeq .activeCritText2,#mermaid-svg-P4Kyuz2sYc2Wfoeq .activeCritText3{fill:#000 !important}#mermaid-svg-P4Kyuz2sYc2Wfoeq .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-P4Kyuz2sYc2Wfoeq g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-P4Kyuz2sYc2Wfoeq g.classGroup text .title{font-weight:bolder}#mermaid-svg-P4Kyuz2sYc2Wfoeq g.clickable{cursor:pointer}#mermaid-svg-P4Kyuz2sYc2Wfoeq g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-P4Kyuz2sYc2Wfoeq g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-P4Kyuz2sYc2Wfoeq .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-P4Kyuz2sYc2Wfoeq .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-P4Kyuz2sYc2Wfoeq .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-P4Kyuz2sYc2Wfoeq .dashed-line{stroke-dasharray:3}#mermaid-svg-P4Kyuz2sYc2Wfoeq #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-P4Kyuz2sYc2Wfoeq #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-P4Kyuz2sYc2Wfoeq #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-P4Kyuz2sYc2Wfoeq #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-P4Kyuz2sYc2Wfoeq #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-P4Kyuz2sYc2Wfoeq #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-P4Kyuz2sYc2Wfoeq #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-P4Kyuz2sYc2Wfoeq #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-P4Kyuz2sYc2Wfoeq .commit-id,#mermaid-svg-P4Kyuz2sYc2Wfoeq .commit-msg,#mermaid-svg-P4Kyuz2sYc2Wfoeq .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-P4Kyuz2sYc2Wfoeq .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-P4Kyuz2sYc2Wfoeq .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-P4Kyuz2sYc2Wfoeq g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-P4Kyuz2sYc2Wfoeq g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-P4Kyuz2sYc2Wfoeq g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-P4Kyuz2sYc2Wfoeq g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-P4Kyuz2sYc2Wfoeq g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-P4Kyuz2sYc2Wfoeq g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-P4Kyuz2sYc2Wfoeq .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-P4Kyuz2sYc2Wfoeq .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-P4Kyuz2sYc2Wfoeq .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-P4Kyuz2sYc2Wfoeq .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-P4Kyuz2sYc2Wfoeq .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-P4Kyuz2sYc2Wfoeq .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-P4Kyuz2sYc2Wfoeq .edgeLabel text{fill:#333}#mermaid-svg-P4Kyuz2sYc2Wfoeq .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-P4Kyuz2sYc2Wfoeq .node circle.state-start{fill:black;stroke:black}#mermaid-svg-P4Kyuz2sYc2Wfoeq .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-P4Kyuz2sYc2Wfoeq #statediagram-barbEnd{fill:#9370db}#mermaid-svg-P4Kyuz2sYc2Wfoeq .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-P4Kyuz2sYc2Wfoeq .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-P4Kyuz2sYc2Wfoeq .statediagram-state .divider{stroke:#9370db}#mermaid-svg-P4Kyuz2sYc2Wfoeq .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-P4Kyuz2sYc2Wfoeq .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-P4Kyuz2sYc2Wfoeq .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-P4Kyuz2sYc2Wfoeq .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-P4Kyuz2sYc2Wfoeq .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-P4Kyuz2sYc2Wfoeq .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-P4Kyuz2sYc2Wfoeq .note-edge{stroke-dasharray:5}#mermaid-svg-P4Kyuz2sYc2Wfoeq .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-P4Kyuz2sYc2Wfoeq .error-icon{fill:#522}#mermaid-svg-P4Kyuz2sYc2Wfoeq .error-text{fill:#522;stroke:#522}#mermaid-svg-P4Kyuz2sYc2Wfoeq .edge-thickness-normal{stroke-width:2px}#mermaid-svg-P4Kyuz2sYc2Wfoeq .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-P4Kyuz2sYc2Wfoeq .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-P4Kyuz2sYc2Wfoeq .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-P4Kyuz2sYc2Wfoeq .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-P4Kyuz2sYc2Wfoeq .marker{fill:#333}#mermaid-svg-P4Kyuz2sYc2Wfoeq .marker.cross{stroke:#333}
:root { --mermaid-font-family: "trebuchet ms", verdana, arial;}
#mermaid-svg-P4Kyuz2sYc2Wfoeq {
color: rgba(0, 0, 0, 0.75);
font: ;
}
Python--ArcGIS
Python
获得数据
分析数据格式
获得需求
工具准备
Microsoft Office Excel
pandas
Python
ArcGIS
初步处理
对于需求1的处理
对于需求2的处理
需求达成
4 数据处理
4.1 初步处理
根据需求与数据进行初步处理:
删除了与需求无关的列对于“当前时间,当前位置”,通过整体复制保存为txt文件,运用替换删除“[”、“]”、“ " ”,再保存为.csv文件,复制回原数据中。再利用Excel中的mid()函数对地址串进行截取,获得行政区数据列。
以下是经初步处理后的数据格式与样例数据:
4.2 Python处理
4.2.1 对于 需求1 的 Python 处理
import pandas
from pandas
import DataFrame
titles
=[]
for i
in range(1,11,1):
titles
.append
('主要植物'+str(i
)+'号名称')
df
=pandas
.read_excel
('房前屋后的物种多样性调查(6).xls','总表',index_col
=0,header
=0)
name_list
=[]
for i
in titles
:
for j
in df
[i
].values
.tolist
():
name_list
.append
(j
)
name_dist
={}
for i
in name_list
:
if i
not in name_dist
:
name_dist
[i
]=1
else:
name_dist
[i
]+=1
tuple_list
=zip(name_dist
.keys
(),name_dist
.values
())
sort
= sorted(tuple_list
,key
=lambda x
: x
[1],reverse
=True)
mask_list
=[]
counter
=0
for i
in sort
:
mask_list
.append
(i
[0])
counter
+=1
if counter
==5:
break
print(mask_list
)
for i
in mask_list
:
mask
=df
[titles
].values
==i
df_result
=DataFrame
(df
[mask
])
filename
=i
+'.csv'
df_result
.to_csv
(filename
)
4.2.2 对于 需求1 的地理处理
利用上述代码输出的CSV文件,在ArcMap的目录窗口中,右击CSV文件,选择“创建要素类”—“从XY表”即可。
4.2.3 对于 需求2 的 Python 处理
import pandas
from pandas
import DataFrame
titles
=[]
for i
in range(1,11,1):
titles
.append
('主要植物'+str(i
)+'号名称')
df
=pandas
.read_excel
('房前屋后的物种多样性调查(6).xls','总表',index_col
=0,header
=0)
dis
=df
['行政区'].values
.tolist
()
temp_dic
={}
for i
in dis
:
if i
not in temp_dic
:
temp_dic
[i
] = 1
else:
temp_dic
[i
] += 1
dis
=list(temp_dic
.keys
())
dis_count
={}
def xls_find(district
):
print(district
)
mask
=df
['行政区'].values
==district
df_district
=df
[mask
]
name_list
= []
for i
in titles
:
for j
in df_district
[i
].values
.tolist
():
name_list
.append
(j
)
name_dist
= {}
for i
in name_list
:
if i
not in name_dist
:
name_dist
[i
] = 1
else:
name_dist
[i
] += 1
dis_count
[district
]=len(name_dist
)
df_temp
=DataFrame
.from_dict
(data
=name_dist
,orient
='index',columns
=['数量'])
df_temp
.sort_values
(by
=['数量'], ascending
=False, inplace
=True)
print(df_temp
)
filename
=district
+'.csv'
df_temp
.to_csv
(filename
, encoding
='utf_8_sig')
for i
in dis
:
xls_find
(i
)
dis_count
=DataFrame
.from_dict
(data
=dis_count
,orient
='index',columns
=['数量'])
dis_count
.sort_values
(by
=['数量'], ascending
=False, inplace
=True)
print(dis_count
)
dis_count
.to_csv
('各区物种多样性值.csv', encoding
='utf_8_sig')
4 分析结果·需求达成
最终成果包括一些6个分布最多的植物的详细数据CSV文件、Shapefile文件与分布图,各区物种多样性详细数据CSV文件。