【python – 如何获得索引的排列?】教程文章相关的互联网学习教程文章

python – ValueError:值的长度与索引的长度不匹配Pandas DataFrame.unique()【代码】

我正在尝试获取新数据集,或将当前数据集列的值更改为其唯一值.这是我想要得到的一个例子:A B----- 0| 1 1 1| 2 5 2| 1 5 3| 7 9 4| 7 9 5| 8 9Wanted Result Not Wanted ResultA B A B----- -----0| 1 1 0| 1 11| 2 5 1| 2 52| 7 9 2| 3| 8 3| 7 94|5| 8我真的不关心索引,但似乎是问题所在.到目前为止,我的代码非常简单,我尝试了两种方法,一种是使用新的dataFrame而另一...

python – 具有多索引列的Pandas数据框 – 合并级别【代码】

我有一个数据框,分组,多指数列如下:import pandas as pd codes = ["one","two","three"]; colours = ["black", "white"]; textures = ["soft", "hard"]; N= 100 # length of the dataframe df = pd.DataFrame({ 'id' : range(1,N+1),'weeks_elapsed' : [random.choice(range(1,25)) for i in range(1,N+1)],'code' : [random.choice(codes) for i in range(1,N+1)],'colour': [random.choice(colours) for i in range(1,N+1)],'te...

numpy数组数组的pythonic方法(带行索引)【代码】

我想在表中找到与特定索引相对应的值.例如,这是我的表:import numpy as np my_array = np.array([[0,1,0,1,0,1,0],[1,2,1,2,1,2,1],[4,5,4,3,3,4,5]])#--------------------------------------------------------------------- # my_array : [[0, 1, 0, 1, 0, 1, 0], # [1, 2, 1, 2, 1, 2, 1], # [4, 5, 4, 3, 3, 4, 5]])以下是一系列索引.此数组中的值是my_array的行. (列未编制索...

python – Pandas中双括号`[[…]]`和单括号`[..]`索引之间的区别【代码】

我对以下代码行的语法感到困惑:x_values = dataframe[['Brains']]数据框对象由2列(大脑和身体)组成Brains Bodies 42 34 32 23当我打印x_values时,我得到这样的东西:Brains 0 42 1 32就dataframe对象的属性和方法而言,我知道pandas文档,但是双括号语法让我感到困惑.解决方法:考虑一下: 来源DF:In [79]: df Out[79]:Brains Bodies 0 42 34 1 32 23选择一列 – 导致Pandas.Series:In [80]: df['B...

python – 在读取csv时删除pandas中的索引列【代码】

我有以下代码导入CSV文件.有3列,我想将前两个设置为变量.当我将第二列设置为变量“效率”时,索引列也会被添加.我怎样才能摆脱索引列?df = pd.DataFrame.from_csv('Efficiency_Data.csv', header=0, parse_dates=False) energy = df.index efficiency = df.Efficiency print efficiency我试过用del df['index']在我设定之后energy = df.index我在另一篇文章中找到但导致“KeyError:’index’”解决方法:DataFrames和Series始终具有...

python – 在NumPy数组中查找等于零的元素索引【代码】

NumPy具有有效的函数/方法nonzero()来识别ndarray对象中的非零元素的索引.获取值为零的元素的索引的最有效方法是什么?解决方法:numpy.where()是我的最爱.>>> x = numpy.array([1,0,2,0,3,0,4,5,6,7,8]) >>> numpy.where(x == 0)[0] array([1, 3, 5])

Python分布式爬虫打造搜索引擎【图】

第1章 课程介绍 第2章 windows下搭建开发环境 第3章 爬虫基础知识回顾 第4章 scrapy爬取知名技术文章网站 第5章 scrapy爬取知名问答网站 第6章 通过CrawlSpider对招聘网站进行整站爬取 第7章 Scrapy突破反爬虫的限制 第8章 scrapy进阶开发 第9章 scrapy-redis分布式爬虫 第10章 elasticsearch搜索引擎的使用 第11章 django搭建搜索网站 第12章 scrapyd部署scrapy爬虫 第13章 课程总结点击下载全套视频课程 提取码:yzz7 更多精彩内...

python – 按索引访问collections.OrderedDict中的项目【代码】

可以说我有以下代码:import collections d = collections.OrderedDict() d['foo'] = 'python' d['bar'] = 'spam'有没有办法以编号的方式访问这些项目,例如:d(0) #foo's Output d(1) #bar's Output解决方法:如果它是一个OrderedDict(),您可以通过获取(键,值)对的元组进行索引来轻松访问元素,如下所示>>> import collections >>> d = collections.OrderedDict() >>> d['foo'] = 'python' >>> d['bar'] = 'spam' >>> d.items() [('f...

使用索引获取项目,Python【代码】

我有一个python中的元组(‘A’,’B’,’C’,’D’,’E’),我如何获得特定索引号下的项目? 例:假设它被赋予0,它将返回A.给定2,它将返回C.给定4,它将返回E.解决方法:你所展示的,(‘A’,’B’,’C’,’D’,’E’)不是一个列表,它是一个元组(圆括号而不是方括号表示).然而,无论是索引列表还是元组索引(用于在索引处获取一个项目),在任何一种情况下都将索引附加在方括号中. 所以:thetuple = ('A','B','C','D','E') print thetuple[0]...

Python *中的默认切片索引是什么*?【代码】

从python文档docs.python.org/tutorial/introduction.html#strings:Slice indices have useful defaults; an omitted first index defaults to zero, an omitted second index defaults to the size of the string being sliced.对于标准情况,这很有意义:>>> s = 'mystring' >>> s[1:] 'ystring' >>> s[:3] 'mys' >>> s[:-2] 'mystri' >>> s[-1:] 'g' >>> 到现在为止还挺好.但是,使用负步长值似乎表明默认值略有不同:>>> s[:3:-...

为什么python的列表切片不会产生索引超出范围的错误?【代码】

参见英文答案 > Why does substring slicing with index out of range work? 3个在使用数组切片时,我注意到[index:]或[:index]切片类型不会为字符串生成数组索引超出绑定的错误.str = "abcde" print str[10:] print str[:10]产生输出:'' abcde谁能解释为什么?它不应该产生数组索引越界错误?如果我尝试执行以下操作,Python确实会产生此错误:print str [10].解决方法:切片用于创建新列表.如...

python – 一次获取NumPy数组中的几个元素的索引【代码】

有没有办法一次获取NumPy数组中的几个元素的索引? 例如.import numpy as np a = np.array([1, 2, 4]) b = np.array([1, 2, 3, 10, 4])我想找到a中每个元素的索引,即:[0,1,4]. 我发现我使用的解决方案有点冗长:import numpy as npa = np.array([1, 2, 4]) b = np.array([1, 2, 3, 10, 4])c = np.zeros_like(a) for i, aa in np.ndenumerate(a):c[i] = np.where(b==aa)[0]print('c: {0}'.format(c))输出:c: [0 1 4]解决方法:您可...

python – 分配(而不是定义)一个__getitem__魔术方法打破了索引【代码】

参见英文答案 > Why won’t dynamically adding a `__call__` method to an instance work? 2个我有一个类似于这个(强烈简化)示例的包装类:class wrap(object):def __init__(self):self._data = range(10)def __getitem__(self, key):return self._data.__getitem__(key)我可以像这样使用它:w = wrap() print w[2] # yields "2"我以为我可以通过改变这个来优化和摆脱一个函数调用:class wra...

python – 为什么用括号和逗号对索引的numpy数组的行为有所不同?【代码】

我倾向于使用括号来索引numpy数组(矩阵),但我注意到当我想切片数组(矩阵)时我必须使用逗号表示法.为什么是这样?例如,>>> x = numpy.array([[1, 2], [3, 4], [5, 6]]) >>> x array([[1, 2],[3, 4],[5, 6]]) >>> x[1][1] 4 # expected behavior >>> x[1,1] 4 # expected behavior >>> x[:][1] array([3, 4]) # huh? >>> x[:,1] array([2, 4, 6]) # expected behavior解决方法:这个:x[:, 1]表...

python – 将数组排序到索引数组指定的bin中的最有效方法?【代码】

任务示例:data = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9]) idx = np.array([2, 0, 1, 1, 2, 0, 1, 1, 2])预期结果:binned = np.array([2, 6, 3, 4, 7, 8, 1, 5, 9])约束: >应该快.>应为O(n k),其中n是数据长度,k是bin的数量.>应该是稳定的,即保留在箱内的订单. 明显的解决方案data[np.argsort(idx, kind='stable')]是O(n log n). O(n k)解决方案def sort_to_bins(idx, data, mx=-1):if mx==-1:mx = idx.max() + 1cnts = np.zero...