我想读取dbfArcGIS shapefile的文件并将其转储到pandas数据框中.我目前正在使用dbf包.
我显然能够将dbf文件作为表加载,但是无法弄清楚如何解析它并将其转换为pandas数据帧.这样做的方法是什么?
这是我被困在的地方:
import dbf
thisTable = dbf.Table('C:\\Users\\myfolder\\project\\myfile.dbf')
thisTable.open(mode='read-only')
Run Code Online (Sandbox Code Playgroud)
Python将此语句作为输出返回,我坦率地不知道该怎么做:
dbf.ver_2.Table('C:\\Users\\myfolder\\project\\myfile.dbf', status='read-only')
编辑
我原来的样本dbf:
FID Shape E N
0 Point 90089.518711 -201738.245555
1 Point 93961.324059 -200676.766517
2 Point 97836.321204 -199614.270439
... ... ... ...
Run Code Online (Sandbox Code Playgroud) 我的目标是在Python中创建加拿大的等值线图.假设我有一个字典,其中的值指的是每个加拿大省/地区:
myvalues={'Alberta': 1.0,
'British Columbia': 2.0,
'Manitoba': 3.0,
'New Brunswick': 4.0,
'Newfoundland and Labrador': 5.0,
'Northwest Territories': 6.0,
'Nova Scotia': 7.0,
'Nunavut': 8.0,
'Ontario': 9.0,
'Prince Edward Island': 10.0,
'Quebec': 11.0,
'Saskatchewan': 12.0,
'Yukon': 13.0}
Run Code Online (Sandbox Code Playgroud)
现在我想myvalues使用连续的色彩图(例如,红色阴影)基于相应的值为每个省着色.怎么做?
到目前为止,我只能在matplotlib中绘制加拿大各省/地区,但它们的形状呈现出独特的颜色,我不知道如何根据数字改变它myvalues(也许我需要玩,patches但我不知道怎么样).
您可以在这里找到shapefile:http://www.filedropper.com/canadm1_1
这是我迄今为止的代码:
import shapefile
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection
# -- input --
sf = shapefile.Reader("myfolder\CAN_adm1.shp")
recs = sf.records()
shapes …Run Code Online (Sandbox Code Playgroud) 我使用的是Windows 8.1 64位和Python 2.7.在尝试scikit-image从shell 安装时
pip install scikit-image
我遇到过这个错误:
Command "python setup.py egg_info" failed with error code 1 in c:\users\france~1\appdata\local\temp\pip-buildtksnfe\scikit-image\
下载很好,但安装失败.这里有什么问题以及如何解决?
编辑
升级我的点子后
python -m pip install -U pip setuptools
再试一次,我得到了:
Command "python setup.py egg_info" failed with error code 1 in c:\users\france~1\appdata\local\temp\pip-build-nbemct\scikit-image\
怎么了?
我是xml解析的新手.此xml文件具有以下树:
FHRSEstablishment
|--> Header
| |--> ...
|--> EstablishmentCollection
| |--> EstablishmentDetail
| | |-->...
| |--> Scores
| | |-->...
|--> EstablishmentCollection
| |--> EstablishmentDetail
| | |-->...
| |--> Scores
| | |-->...
Run Code Online (Sandbox Code Playgroud)
但是当我使用ElementTree访问它并查找child标签和属性时,
import xml.etree.ElementTree as ET
import urllib2
tree = ET.parse(
file=urllib2.urlopen('http://ratings.food.gov.uk/OpenDataFiles/FHRS408en-GB.xml' % i))
root = tree.getroot()
for child in root:
print child.tag, child.attrib
Run Code Online (Sandbox Code Playgroud)
我只得到:
Header {}
EstablishmentCollection {}
Run Code Online (Sandbox Code Playgroud)
我认为这意味着他们的属性是空的.为什么会这样,我怎么能访问内部嵌套孩子EstablishmentDetail和Scores?
编辑
感谢下面的答案,我可以进入树内,但如果我想要检索诸如此类的值,则会Scores失败:
for node in root.find('.//EstablishmentDetail/Scores'): …Run Code Online (Sandbox Code Playgroud) 我有一个list:
L = ['a', 'b']
Run Code Online (Sandbox Code Playgroud)
我需要创建一个新的list通过串联原始list范围从去1到k.例:
k = 4
L1 = ['a1','b1', 'a2','b2','a3','b3','a4','b4']
Run Code Online (Sandbox Code Playgroud)
我尝试:
l1 = L * k
print l1
#['a', 'b', 'a', 'b', 'a', 'b', 'a', 'b']
l = [ [x] * 2 for x in range(1, k + 1) ]
print l
#[[1, 1], [2, 2], [3, 3], [4, 4]]
l2 = [item for sublist in l for item in sublist]
print l2
#[1, 1, 2, 2, …Run Code Online (Sandbox Code Playgroud) 我希望这不是微不足道的,但我想知道以下内容:
如果我有一个包含n个 csv文件的特定文件夹,我怎么能一次一个地迭代读取所有文件,并对它们的值执行一些计算?
例如,对于单个文件,我执行类似的操作并对x数组执行一些计算:
import csv
import os
directoryPath=raw_input('Directory path for native csv file: ')
csvfile = numpy.genfromtxt(directoryPath, delimiter=",")
x=csvfile[:,2] #Creates the array that will undergo a set of calculations
Run Code Online (Sandbox Code Playgroud)
我知道我可以检查csv给定文件夹中有多少文件(请点击此处):
import glob
for files in glob.glob("*.csv"):
print files
Run Code Online (Sandbox Code Playgroud)
但是我没有弄清楚如何将numpy.genfromtxt()函数嵌套在for循环中,所以我读入了一个目录的所有csv文件,由我来指定.
编辑
我只有的文件夹jpg和csv文件.后者是命名的eventX.csv,其中X的范围是1到50.因此,for我所指的循环应该按照它们的方式考虑文件名.
我似乎在确定如何在使用时增加或减少fontsizex和y刻度标签时遇到问题matplotlib.
我知道有set_xticklabels(labels, fontdict=None, minor=False, **kwargs)功能,但我无法理解如何控制fontsize它.
我期待一些明确的东西,比如
title_string=('My Title')
plt.suptitle(title_string, y=1.0, fontsize=17)
Run Code Online (Sandbox Code Playgroud)
但到目前为止我还没有找到类似的东西.我错过了什么?
我正在尝试绘制几条线(不是条形图,在这种情况下)。我的 y 值是float,而 x 值是categorical data。如何做到这一点matplotlib?
我的价值观:
data1=[5.65,7.61,8.17,7.60,9.54]
data2=[7.61,16.17,16.18,19.54,19.81]
data3=[29.55,30.24,31.51,36.40,35.47]
Run Code Online (Sandbox Code Playgroud)
我的类别:
x_axis=['A','B','C','D','E']
Run Code Online (Sandbox Code Playgroud)
我正在使用的代码,它没有给我我想要的:
import matplotlib.pyplot as plt
fig=plt.figure() #Creates a new figure
ax1=fig.add_subplot(111) #Plot with: 1 row, 1 column, first subplot.
line1 = ax1.plot(str(x_axis), data1,'ko-',label='line1') #Plotting data1
line2 = ax1.plot(str(x_axis), data2,'ro-',label='line2') #Plotting data2
line3 = ax1.plot(str(x_axis), data3,'mo-',label='line3') #Plotting data3
plt.xticks(range(len(data3)), x_axis, size='small')
ax1.set_ylim(0,51)
ax1.set_ylabel('y values',fontsize=12)
#Assigning labels
lines = line1+line2+line3
labels = [l.get_label() for l in lines]
ax1.legend(lines,labels,loc='upper center', prop={'size':10}, …Run Code Online (Sandbox Code Playgroud) 我有两个地理坐标列表(lat,long),我需要将它们组合成一个2D数组或矩阵.
它们现在存储在数据帧中:
lat lon
0 48.010258 -6.156909
1 48.021648 -6.105887
2 48.033028 -6.054801
3 48.044384 -6.003691
4 48.055706 -5.952602
5 48.067017 -5.901447
6 48.078304 -5.850270
7 48.089558 -5.799114
8 48.100800 -5.747891
Run Code Online (Sandbox Code Playgroud)
如何将这两个列表组合成一个2D数组,以便保留lat-lon对应关系?这些是简单的数据:
lat=[48.01,48.02,48.03,48.04,48.05,48.06,48.07,48.08,48.10]
lon=[-6.15,-6.10,-6.05,-6.00,-5.95,-5.90,-5.85,-5.79,-5.74]
Run Code Online (Sandbox Code Playgroud)
编辑
这些摘录数据代表(纬度,长度)或(y,x)地理地图.结合起来,它们重现下面的图像.您清楚地看到存在的预期结果将被剥夺一定宽度的外部数据框架.因此,它就像切出图片的外框,其宽度为30个数据点.
说,你有3个numpy的数组:lat,lon,val:
import numpy as np
lat=np.array([[10, 20, 30],
[20, 11, 33],
[21, 20, 10]])
lon=np.array([[100, 102, 103],
[105, 101, 102],
[100, 102, 103]])
val=np.array([[17, 2, 11],
[86, 84, 1],
[9, 5, 10]])
Run Code Online (Sandbox Code Playgroud)
并且假设您要创建一个pandas数据框df.columns = ['lat', 'lon', 'val'],但由于每个值lat都与a long和val数量相关联,因此您希望它们出现在同一行中.
此外,您希望每列的按行顺序跟随每个数组中的位置,以便获取以下数据帧:
lat lon val
0 10 100 17
1 20 102 2
2 30 103 11
3 20 105 86
... ... ... ...
Run Code Online (Sandbox Code Playgroud)
所以基本上数据帧中的第一行存储每个数组的"第一"数量,依此类推.这该怎么做?
我无法找到这样做的pythonic方式,所以任何帮助都将非常感激.
python ×10
matplotlib ×3
arrays ×2
dataframe ×2
numpy ×2
pandas ×2
arcgis ×1
axis-labels ×1
choropleth ×1
csv ×1
dbf ×1
elementtree ×1
for-loop ×1
genfromtxt ×1
installation ×1
kwargs ×1
list ×1
patch ×1
pip ×1
plot ×1
python-2.7 ×1
range ×1
scikit-image ×1
shapefile ×1
text ×1
tree ×1
xml ×1
xml-parsing ×1