【python-当前在Google App Engine上存储,索引和查询地理空间数据的选项是什么?】教程文章相关的互联网学习教程文章

python – 倒置的花式索引【代码】

使用花式索引具有该阵列的数组和掩码,很容易仅选择与掩码对应的数组的数据.import numpy as npa = np.arange(20).reshape(4, 5) mask = [0, 2] data = a[:, mask]但是有没有一种快速的方法可以选择不属于掩码的阵列的所有数据(即掩码是我们要拒绝的数据)?我试图找到一个通过中间布尔数组的通用解决方案,但我确信有一些东西真的很容易.mask2 = np.ones(a.shape)==1 mask2[:, mask]=False data = a[mask2].reshape(a.shape[0], a.sh...

python – 使用记录中的pandas索引几个csv文件?【代码】

我有一个csv文件列表(“file1”,“file2”,…“)有两列但没有标题标签.我想给它们分配标题标签,它们作为一个DataFrame,由索引文件,然后由这些列标签索引.例如,我尝试:import pandasmydict = {} labels = ["col1", "col2"] for myfile in ["file1", "file2"]:my_df = pandas.read_table(myfile, names=labels)# build dictionary of dataframe recordsmydict[myfile] = my_dftest = pandas.DataFrame(mydict)这会生成一个DataFrame...

python – Pandas中数组的多索引,具有非唯一数据【代码】

我有以下数据框架In[45]: data[:10] Out[45]:Z A beta2 M shell 0 100 200 0.3112 197.2 -4.213 1 100 200 -0.4197 202 -1.143 2 100 200 0.03205 203 0 3 100 201 0.2967 191 -4.434 4 100 201 -0.4893 196.1 -4.691 5 100 202 0.3084 183.4 -4.134 6 100 202 -0.4873 188.2 -4.75 7 100 202 -0.2483 188.4 -1.106 8 100 203 0.3069 177.1 -4.355 9 101 2...

python – 重新索引或重新排序组【代码】

我正在寻找一种干净的方法来重新排序组中的索引.示例代码:import numpy as np import pandas as pdmydates = pd.date_range('1/1/2012', periods=1000, freq='D') myts = pd.Series(np.random.randn(len(mydates)), index=mydates) grouped = myts.groupby(lambda x: x.timetuple()[7]) mymin = grouped.min() mymax = grouped.max()以上给了我想要的东西,在一年中的朱利安日汇总统计数据但是我想重新排序该组,以便将上半场(183天...

python – Pandas中日期序列(不是索引)的算术【代码】

(Python 2.7,Pandas 0.9) 这似乎是一件简单的事情,但我无法弄清楚如何使用Pandas计算数据框中两个日期列之间的差异.此数据帧已经有一个索引,因此不希望将任何列放入DateTimeIndex. 要从我使用的字符串转换每个日期列:data.Date_Column = pd.to_datetime(data.Date_Column)从那里,为了获得两列之间的经过时间,我做:data.Closed_Date - data.Created_Date 返回错误:TypeError: %d format: a number is required, not a numpy.time...

python – 使用相同的列,不同的索引级别对齐DataFrame【代码】

我有两个pandas DataFrames – weight在Land Use列上有一个简单的Index.浓度具有土地利用和参数的MultiIndex.import pandas from io import StringIOconc_string = StringIO("""Land Use,Parameter,1E,1N,1S,2 Airfield,BOD5 (mg/l),0.418,0.118,0.226,1.063 Airfield,Ortho P (mg/l),0.002,0.001,0.001,0.002 Airfield,TSS (mg/l),1.773,11.47,0.862,0.183 Airfield,Zn (mg/l),0.001,0.001,4.95E-05,0.001 "Commercial",BOD5 (m...

Python Pandas在多索引数据帧中访问第二个索引的值【代码】

我不太确定多索引是如何工作的,所以我可能只是想在这里做错事.如果我有一个数据框Value A B 1 1 5.67 1 2 6.87 1 3 7.23 2 1 8.67 2 2 9.87 2 3 10.23如果我想访问B = 2的元素,我该怎么做? df.ix [2]给了我A = 2.为了得到一个特定的值,它似乎是df.ix [(1,2)],但如果你不能直接访问它,这就是B索引的目的?解决方法:你可以使用xs:In [11]: df.xs(2, level='B') Out[11]:Value A 1 6.87 2 9....

python – Pivoting pandas DataFrame – AssertionError:索引长度与值不匹配【代码】

我有一个pandas.DataFrame,它不会像我期望的那样转动.虽然pivot_table正确地安排了所有事情,但它使用聚合函数来实现这一目标的事实令人反感.另外,pivot_table似乎返回一个不必要的复杂对象而不是一个平面数据框. 请考虑以下示例import pandas as pd df = pd.DataFrame({'firstname':['Jon']*3+['Amy']*2,'lastname':['Cho']*3+['Frond']*2,'vehicle':['bike', 'car', 'plane','bike','plane'],'weight':[81.003]*3+[65.6886]*2,'sp...

Python:如果list包含字符串打印列表中包含它的所有索引/元素【代码】

我能够检测到匹配但无法找到它们的位置. 鉴于以下列表:['A second goldfish is nice and all', 3456, 'test nice']我需要搜索匹配(即“nice”)并打印包含它的所有列表元素.理想情况下,如果要搜索的关键字“很好”,结果应该是:'A second goldfish is nice and all' 'test nice'我有:list = data_array string = str(raw_input("Search keyword: ")) print string if any(string in s for s in list):print "Yes"所以它找到匹配并...

numpy python中的“IndexError:索引太多了”【代码】

我知道很多人都问过这个问题,但我找不到能解决问题的合适答案. 我有一个数组X ::X=[1. 2. -10.]现在我试图让矩阵Y读取这个X数组.我的代码是::# make Y matrixY=np.matrix(np.zeros((len(X),2))) i=0while i < len(load_value):if X[i,1] % 2 != 0:Y[i,0] = X[i,0]*2-1elif X[i,1] % 2 == 0:Y[i,0] = X[i,0] * 2Y[i,1] = X[i,2]i = i + 1 print('Y=') print(Y)现在,如果我运行它,它会给出以下错误::Traceback (most recent call l...

python – 从索引列表到单热矩阵【代码】

在Theano中将索引向量转换为零和1的矩阵的最佳(优雅和有效)方法是什么,其中每一行都是索引的一个N表示?v = t.ivector() # the vector of indices n = t.scalar() # the width of the matrix convert = <your code here> f = theano.function(inputs=[v, n], outputs=convert)例:n_val = 4 v_val = [1,0,3] f(v_val, n_val) = [[0,1,0,0],[1,0,0,0],[0,0,0,1]]解决方法:我没有比较不同的选项,但你也可以这样做.它不要求额外的内...

在稀疏lil_matrix(Scipy / Python)中查找最大值及其索引【代码】

在Scipy稀疏lil_matrix对象中找到最大值及其对应的行和列索引的最佳方法是什么?我可以loop through the nonzero entries using itertools.izip,但有什么更好的吗?我觉得我在这里遗漏了一些明显的东西……解决方法:您可以转换为COO格式,然后使用data,row和col属性. 例如,假设LIL矩阵是x.这是获取最大值及其行和列的一种方法:In [41]: x Out[41]: <1000x1000 sparse matrix of type '<type 'numpy.float64'>'with 1999 stored el...

python – 对pandas中的datetime索引的算术运算【代码】

在pandas中,您可以通过基于经典整数位置/行的索引或基于日期时间的索引来访问时间系列的特定位置.可以使用基本算术运算来操纵基于整数的索引,例如,如果我对一个频率为12小时的时间序列有一个integer_index并且我想在此前一天访问该条目,我可以简单地执行integer_index – 2.但是,真实世界数据并不总是完美的,有时行丢失.在这种情况下,此方法失败,并且能够使用基于日期时间的索引和减法(例如,从该索引中的一天)将是有帮助的.我怎样...

Python igraph顶点索引【代码】

我在python中使用igraph库.我想知道是否有一种使用字符串作为顶点索引的方法.我知道’name’属性,我可以写g = igraph.Graph(directed=True) g.add_vertex('hello') g.add_vertex('world') g.add_edge('hello','world')一切正常.除非我添加相同的顶点两次,例如:g = igraph.Graph(directed=True) g.add_vertex('world') g.add_vertex('hello') g.add_vertex('hello')创建了两个不同的顶点,如果我现在添加一个边:g.add_edge('hello'...

从文本文件加载列表中找不到Python列表索引【代码】

分配是让用户输入4个数字,然后将它们存储在文本文件中,打开该文本文件,在不同的行上显示4个数字,然后获得这些数字的平均值并将其显示给用户.到目前为止,这是我的代码:__author__ = 'Luca Sorrentino'numbers = open("Numbers", 'r+') numbers.truncate() #OPENS THE FILE AND DELETES THE PREVIOUS CONTENT# Otherwise it prints out all the inputs into the file evernumbers = open("Numbers", 'a') #Opens the file so that ...