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

python – Pandas在这里做的是什么,我的索引[0]和[1]引用了相同的值?【代码】

我有一个包含这些索引和值的数据框:df[df.columns[0]]1 example2 example13 example2当我访问df [df.columns [0]] [2]时,我得到“example1”.说得通.这就是指数的运作方式. 然而,当我访问df [df.columns [0]]时,我得到了“example”,当我访问df [df.columns [1]]时我得到了例子.因此对于df[df.columns[0]][0]df[df.columns[0]][1]我得到“榜样”. 奇怪的是,我可以删除“row”0,结果是1被删...

python – Pandas Dataframe – 多级索引/列【代码】

我在组织数据帧时遇到了一些困难.我认为这很简单,但我已经坚持了这么久: 这是df1:Output Energy, (Wh/h) Lights (Wh) Lights+Media (Wh) Total Usage (h) Hour 1 0.0 0.0 0.0 0.0 2 0.0 0.0 0.0 0.0 3 ...

python – 为每个索引应用pandas groupby【代码】

我有一个数据框,其中一个人的名字作为索引(可以有多个条目)和两列“X”和“Y”.列’X’和’Y’可以是A-C之间的任何字母. 例如:df = pd.DataFrame({'X' : ['A', 'B', 'A', 'C'], 'Y' : ['B', 'A', 'A', 'C']},index = ['Bob','Bob','John','Mike'])对于每个人(即索引),我想得到列’X’和’Y’的每个唯一组合的出现次数(例如 – 对于Bob我有1个计数(‘A’,’B’)和1计数(‘B’,’A’)). 当我执行以下操作时:df.loc['Bob'].groupby(...

python – Pandas数据透视表ValueError:索引包含重复的条目,无法重新整形【代码】

我有一个如下所示的数据帧(前3行):Sample_Name Sample_ID Sample_Type IS Component_Name IS_Name Component_Group_Name Outlier_Reasons Actual_Concentration Area Height Retention_Time Width_at_50_pct Used Calculated_Concentration Accuracy Index 1 20170824_ELN147926_HexLacCer_Plasma_A-1-1 NaN Unknown True GluCer(d1...

python – 将索引拆分为pandas中的单独列【代码】

我有一个大型数据框,从中我可以获得groupby所需的数据.我需要从新数据帧的索引中获取几个单独的列. 部分原始数据框如下所示:code place vl year week 0 111.0002.0056 region1 1 2017 29 1 112.6500.2285 region2 1 2017 31 2 112.5600.6325 region2 1 2017 30 3 112.5600.6325 region2 1 2017 30 4 112.5600.8159 region2 1 2017 30 5 111...

python – 用基于索引的其他DataFrame替换Pandas DataFrame中的行【代码】

我有2个dataFramesdf1B C A 0 300 6 1 400 7 2 500 8 3 600 9df2B C A 2 433 99 3 555 99这就是我构建它们的方式:df1 = pd.DataFrame({'A': [0, 1, 2, 3],'B': [300, 400, 500, 600], 'C': [6, 7, 8, 9]}) df1.set_index('A', inplace=True) df2 = pd.DataFrame({'A': [2, 3],'B': [433, 555],'C': [99, 99]}) df2.set_index('A', inplace=True)我想根据索引将df1中的所有行替换为df2中的行,结果应...

列表索引在python中超出范围【代码】

import glob import xlrdfrom xlwt import Workbookwb = Workbook()for file_name in glob.glob("foo*.xls"):wb_orig = xlrd.open_workbook(file_name)for ws_orig in wb_orig.sheets():ws = wb.add_sheet('{0} {1}'.format(file_name, ws_orig.name))for rx in range(ws_orig.nrows):for cx in range(ws_orig.ncols):ws.write(rx, cx, ws_orig.cell_value(rx,cx))wb.save("mefoo.xls")我在很多方面尝试了上面的代码,将多个excel表...

python – 无法理解matplotlib的示例,其中省略号和冒号可能与索引相关联【代码】

我有一个关于这个matplotlib的example的问题. 这是我不理解的部分def update_line(num, data, line):line.set_data(data[...,:num])return line,line.set_data(data […,:num])有什么作用?解决方法:这是numpy提供的一种特殊语法,用于在多维数组中进行切片.一般语法是[s1,s2,…,sn],其中si是用于通常切片或索引序列的表达式,并且在第i维度中定义期望的切片.例如,[5,2:3,1 :: 2]. ……是缩短所有尺寸的完整切片的缩短.例如,如果a是...

带有字符串索引的Python 2 Dimension Array(Matrix)【代码】

在Python(2.7)中,是否存在可通过基于字符串的索引访问的本地二维数据结构? 我知道你可以拥有一个可以用字符串索引访问的字典,例如:>>> dic = dict() >>> dic['grumpy'] = 'cat' >>> print(dict['grumpy']) 'cat'但我想要的是一个可以访问的数据结构,如:>>> dic['grumpy']['frumpy'] = 'cat' >>> print(dict['grumpy']['frumpy']) 'cat'数组似乎是禁止的,因为它只允许基于整数的访问…任何建议?谢谢!解决方法:使用defaultdict:...

python – 从基于索引的numpy数组中获取子数组【代码】

我有一个numpy数组向量,我想得到一个基于索引的子集:import numpy as np input=np.array([1,2,3,4,5,6,7,8,9,10]) index=np.array([0,1,0,0,0,0,1,0,0,1])什么是pythonic方法来输出输出= [2,7,10]?解决方法: output = input[index.astype(np.bool)]要么output = input[np.where(index)[0]]

python – 错误:列表索引必须是整数而不是浮点数【代码】

下面的代码应该从学生的字典中获取标记列表并计算学生的平均分数.我得到“TypeError:list indices必须是整数,而不是float”错误.alice = {"name": "Alice","homework": [100.0, 92.0, 98.0, 100.0],"quizzes": [82.0, 83.0, 91.0],"tests": [89.0, 97.0] }# Averege function is given below for calculating avg def average(lst):l=float(len(lst))total = 0.0#code works till here the error occoured belowfor item in lst:a...

Python中的循环数组索引【代码】

我希望得到一个圆形的数组(或矩阵),这样 让:a = [1,2,3]然后我想a[0] = 1 a[1] = 2 a[2] = 3 a[3] = 1 a[4] = 2等于a的所有索引值. 原因是因为我有一个图像作为矩阵,我正在尝试处理它的行为,如果它在一个方向上离开边缘,它应该重新出现在另一侧. 任何关于如何干净利落的提示将非常感谢!解决方法:您可以像这样使用模运算符print a[3 % len(a)] 如果你不想像这样使用模运算符,你需要自己子类列表并实现__getitem__.class CustomLis...

python – 展平numpy数组但是还保持值位置的索引?【代码】

我有几个2D numpy数组(矩阵),每个我想将它转换为包含数组值的向量和包含每个行/列索引的向量. 例如,我可能有这样的数组:x = np.array([[3, 1, 4],[1, 5, 9],[2, 6, 5]])我基本上想要这些价值观[3, 1, 4, 1, 5, 9, 2, 6, 5]和他们的立场[[0,0], [0,1], [0,2], [1,0], [1,1], [1,2], [2,0], [2,1], [2,2]]我的最终目标是将这些放入pandas DataFrame中,如下所示:V | x | y --+---+--- 3 | 0 | 0 1 | 0 | 1 4 | 0 | 2 1 | 1 | 0 5 | ...

删除python列表中的重复项但记住索引【代码】

如何删除列表中的重复项,保留项目的原始顺序并记住列表中任何项目的第一个索引? 例如,从[1,1,2,3]中删除重复项会产生[1,2,3],但我需要记住索引[0,2,3]. 我使用的是Python 2.7.解决方法:使用enumerate跟踪索引和一组跟踪元素:l = [1, 1, 2, 3] inds = [] seen = set() for i, ele in enumerate(l):if ele not in seen:inds.append(i)seen.add(ele)如果你想要两个:inds = [] seen = set() for i, ele in enumerate(l):if ele not ...

python – 有效地查找由numpy数组的索引拆分的子数组【代码】

给定一个数组’数组’和一组索引’索引’,如何找到通过以矢量化方式沿着这些索引分割数组而形成的子数组的累积和?澄清一下,假设我有:>>> array = np.arange(20) >>> array array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]) indices = np.arrray([3, 8, 14])操作应该输出:array([0, 1, 3, 3, 7, 12, 18, 25, 8, 17, 27, 38, 50, 63, 14, 29, 45, 62, 80, 99])请注意,阵列非常大(100000...