slice

Slicing and assigning values multi-indexed pandas dataframe of unique sequential indices

馋奶兔 提交于 2019-12-11 01:16:43
问题 I want to select and change the value of a dataframe cell. There are 2 indices used for this dataframe: 'datetime' and 'idx'. Both contain labels which are unique and sequential. 'datetime' index has datetime label of datetime type, and 'idx' has integer valued labels. import numpy as np import pandas as pd dt = pd.date_range("2010-10-01 00:00:00", periods=5, freq='H') d = {'datetime': dt, 'a': np.arange(len(dt))-1,'b':np.arange(len(dt))+1} df = pd.DataFrame(data=d) df.set_index(keys=

Slicing string from start [duplicate]

梦想与她 提交于 2019-12-10 23:51:06
问题 This question already has answers here : Understanding slice notation (32 answers) Closed 4 years ago . In Python: s = "Gwen Stefani" a = s[:0] b = s[0] print type(a) # str print type(b) # str print len(a) # 0 print len(b) # 1 I understand that s[:0] is empty, but it also means from the start of the string so I would have thought that: s[:0] is the same as s[0] 回答1: You can read about the behaviour of string slicing at https://docs.python.org/2/tutorial/introduction.html#strings, which

Match array according to value

时光总嘲笑我的痴心妄想 提交于 2019-12-10 23:34:29
问题 I'm using the following code to parse yaml and should get output as runners object and the function build should change the data structure and provide output according to below struct type Exec struct { NameVal string Executer []string } This is what I have tried but I'm not sure how to replace the hard-code values inside the function runner from the value I'm getting inside the yaml return []Exec{ {"#mytest", []string{"spawn child process", "build", "gulp"}}, } with the data from the parsed

'Assign' 2D block slice in tensorflow?

杀马特。学长 韩版系。学妹 提交于 2019-12-10 23:02:06
问题 Does tensorflow provide a way to create a zero-tensor while replacing a general slice by another one? In particular, I need to assign 2D blocks in a matrix-tensor. Here's an example of what I need to achieve: Given a tensor of variable shape, eg. [1 2 3 4 5 6], and another tensor defining the slice, eg. [0 0 0 0 0 0 1 1 1 0 0 1 1 1 0], the new tensor should look as follows: [0 0 0 0 0 0 1 2 3 0 0 4 5 6 0]. I know there's scatter_nd , but it seems like it can only 'replace' values along a full

Clarify how python del works for lists and slices

£可爱£侵袭症+ 提交于 2019-12-10 22:38:13
问题 Reading the docs: del: Deletion of a target list recursively deletes each target, from left to right. Can you clarify why this code works as it works? a = [1,2,3] c = a[:] del c print a del a[:] print a This prints: [1, 2, 3] [] if a slice is a new list which points to same elements of the original list, then why deletion of variable assignment of a slice does not make a empty and why deletion of unreferenced slice removes elements of a ? UPD # -*- coding: utf-8 -*- a = [1,2,3] c = a[:] def

Different slicing behaviors on left/right hand side of assignment operator

我们两清 提交于 2019-12-10 19:14:45
问题 As a Python newbie coming from the C++ background, the slicing operator in Python (3.4.x) looks ridiculous to me. I just don't get the design philosophy behind the "special rule". Let me explain why I say it's "special". On the one hand, according to the Stack Overflow answer here, the slicing operator creates a (deep) copy of a list or part of the list , i.e. a new list. The link may be old (earlier than python 3.4.x), but I just confirmed the behavior with the following simple experiment

Reversed array slice including the first element [duplicate]

假装没事ソ 提交于 2019-12-10 19:08:58
问题 This question already has answers here : Python reverse-stride slicing (8 answers) Closed last year . Let's say I have: >>> a = [1, 2, 3, 4] And I want to get a reversed slice. Let's say I want the 1st and 0th elements given start_idx = 1 and stop_idx = 0 : [2, 1] Using the slice notation: a[x:y:z] What values do I use for x , y , and z using start_idx and stop_idx ? I've tried: >>> a[start_idx:stop_idx:-1] [2] >>> a[start_idx:stop_idx-1:-1] [] Differentiation: This question is about a slice

GoLang: How to delete an element from a 2D slice?

有些话、适合烂在心里 提交于 2019-12-10 17:57:07
问题 I've recently been messing around with Go and I wanted to see how it would be to delete an element from a two-dimensional slice. For deleting an element from a one-dimensional slice, I can successfully use: data = append(data[:i], data[i+1:]...) However, with a two-dimensional slice, using: data = append(data[i][:j], data[i][j+1:]...) throws the error: cannot use append(data[i][:j], data[i][j+1:]...) (type []string) as type [][]string in assignment Would tackling this require a different

How can I add small 2D array to larger array?

南楼画角 提交于 2019-12-10 17:17:27
问题 I have a larger 2D array, and I would like to add a smaller 2D array. from numpy import * x = range(25) x = reshape(x,(5,5)) print x [[ 0 1 2 3 4] [ 5 6 7 8 9] [10 11 12 13 14] [15 16 17 18 19] [20 21 22 23 24]] y = [66,66,66,66] y = reshape(y,(2,2)) print y [[66 66] [66 66]] I would like to add the values from array y to x starting at 1,1 so that x looks like this: [[ 0 1 2 3 4] [ 5 72 73 8 9] [10 77 78 13 14] [15 16 17 18 19] [20 21 22 23 24]] Is this possible with slicing? Can someone

Slicing elements from a Python list using Boolean indexing

折月煮酒 提交于 2019-12-10 17:07:54
问题 I recently came across this way of slicing a list in Python. I've never seen this one before, so I would like to understand this clearly. I have a list ["Peter", "James", "Mark"] and if I slice it using the boolean value False it returns Peter and if I slice using True it returns James , as given below ["Peter", "James", "Mark"][False] => Peter ["Peter", "James", "Mark"][True] => James I would like to know what happens here and what is this method called as? 回答1: The datamodel hook here is