slice

Why does .loc have inclusive behavior for slices?

*爱你&永不变心* 提交于 2020-01-21 06:23:50
问题 For some reason, the following 2 calls to iloc / loc produce different behavior: >>> import pandas as pd >>> df = pd.DataFrame(dict(A=range(3), B=range(3))) >>> df.iloc[:1] A B 0 0 0 >>> df.loc[:1] A B 0 0 0 1 1 1 I understand that loc considers the row labels, while iloc considers the integer-based indices of the rows. But why is the upper bound for the loc call considered inclusive, while the iloc bound is considered exclusive? 回答1: Quick answer: It often makes more sense to do end

KeyError: “None of [['', '']] are in the [columns]” pandas python

人盡茶涼 提交于 2020-01-20 04:06:02
问题 I would like to slice two columns in my data frame. This is my code for doing this: import pandas as pd df = pd.read_csv('source.txt',header=0) cidf=df.loc[:,['vocab','sumCI']] print(cidf) This is a sample of data: ID vocab sumCI sumnextCI new_diff 450 statu 3.0 0.0 3.0 391 provid 4.0 1.0 3.0 382 prescript 3.0 0.0 3.0 300 lymphoma 2.0 0.0 2.0 405 renew 2.0 0.0 2.0 **Firstly I got this error: ** KeyError: “None of [['', '']] are in the [columns]”' What I have tried: I tried putting a header

Slice notation in Scala?

旧城冷巷雨未停 提交于 2020-01-20 02:11:45
问题 Is there something similar to the slice notation in Python in Scala? I think this is really a useful operation that should be incorporated in all languages. 回答1: scala> import collection.IterableLike import collection.IterableLike scala> implicit def pythonicSlice[A, Repr](coll: IterableLike[A, Repr]) = new { | def apply(subrange: (Int, Int)): Repr = coll.slice(subrange._1, subrange._2) | } pythonicSlice: [A,Repr](coll: scala.collection.IterableLike[A,Repr])java.lang.Object{def apply(subrange

How to get a sublist of a list between two words

混江龙づ霸主 提交于 2020-01-19 13:21:37
问题 Starting from a list like this: words = ['tree', 'water', 'dog', 'soap', 'bike', 'cat', 'bird'] I want to get the sublist between two specified words. For example, if I have the words 'water' and 'bike' I want to get the sublist: words = ['water', 'dog', 'soap', 'bike'] or if the list is words = ['tree', 'water', 'dog', 'soap', 'tree', 'cat', 'bird'] and I put the words 'tree' and 'tree' I want to get this sublist: words = ['tree', 'water', 'dog', 'soap', 'tree'] I have also written a program

How to get a sublist of a list between two words

风格不统一 提交于 2020-01-19 13:20:13
问题 Starting from a list like this: words = ['tree', 'water', 'dog', 'soap', 'bike', 'cat', 'bird'] I want to get the sublist between two specified words. For example, if I have the words 'water' and 'bike' I want to get the sublist: words = ['water', 'dog', 'soap', 'bike'] or if the list is words = ['tree', 'water', 'dog', 'soap', 'tree', 'cat', 'bird'] and I put the words 'tree' and 'tree' I want to get this sublist: words = ['tree', 'water', 'dog', 'soap', 'tree'] I have also written a program

Extracting off-diagonal slice of large matrix

妖精的绣舞 提交于 2020-01-19 07:37:05
问题 I've got a large nxn matrix and would like to take off-diagonal slices of varying sizes. For example: 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 I'd like an R function which, when given the matrix and "width of diagonal slice" would return an nxn matrix of just those values. So for the matrix above and, say, 3, I'd get: 1 x x x x x 1 2 x x x x 1 2 3 x x x x 2 3 4 x x x x 3 4 5 x x x x 4 5 6 At the moment I'm using (forgive me) a for loop which is incredibly slow:

Extracting off-diagonal slice of large matrix

南楼画角 提交于 2020-01-19 07:36:28
问题 I've got a large nxn matrix and would like to take off-diagonal slices of varying sizes. For example: 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 I'd like an R function which, when given the matrix and "width of diagonal slice" would return an nxn matrix of just those values. So for the matrix above and, say, 3, I'd get: 1 x x x x x 1 2 x x x x 1 2 3 x x x x 2 3 4 x x x x 3 4 5 x x x x 4 5 6 At the moment I'm using (forgive me) a for loop which is incredibly slow:

Extract arbitrarily rotated plane of data from 3D array as 2D array

孤街浪徒 提交于 2020-01-19 04:00:08
问题 I have a 3D matrix of data in matlab, but I want to extract an arbitrarily rotated slice of data from that matrix and store it as a 2D matrix, which I can access. Similar to how the slice() function displays data sliced at any angle, except I would also like to be able to view and modify the data as if it were an array. I have the coordinates of the pivot-point of the plane as well as the angles of rotation (in x, y and z axis), I have also calculated the equation of the plane in the form: Ax

Python pandas slice dataframe by multiple index ranges

走远了吗. 提交于 2020-01-18 04:41:06
问题 What is the pythonic way to slice a dataframe by more index ranges (eg. by 10:12 and 25:28 )? I want this in a more elegant way: df = pd.DataFrame({'a':range(10,100)}) df.iloc[[i for i in range(10,12)] + [i for i in range(25,28)]] Result: a 10 20 11 21 25 35 26 36 27 37 Something like this would be more elegant: df.iloc[(10:12, 25:28)] 回答1: You can use numpy's r_ "slicing trick": df = pd.DataFrame({'a':range(10,100)}) df.iloc[pd.np.r_[10:12, 25:28]] Gives: a 10 20 11 21 25 35 26 36 27 37 回答2:

Golang对元素slice并去重

不想你离开。 提交于 2020-01-17 20:05:09
参考博客: https://blog.csdn.net/qq_27068845/article/details/77407358 封装的类似PHP的array_column // ArrayColumn 获取二维数组某一个键值 func ArrayColumn(input []map[string]interface{},columnKey string) ([]interface{}){ if len(input)==0 { return []interface{}{} } result := []interface{}{} // for i := 0; i < len(input); i++ { // result = append(result, input[i][columnKey]) // } for _,v := range input { fmt.Print(v[columnKey]) result = append(result, v[columnKey]) } return result } // ArrayUnique 数组去重 func ArrayUnique(input []string)([]string){ result := []string{} // 存放不重复主键 tempMap := map[string]byte{} for _, e :=