in-place

Inplace transformation pandas with groupby

自古美人都是妖i 提交于 2019-11-29 16:05:02
Would it be possible to mutate DataFrame inplace with groupby statement? import pandas as pd dt = pd.DataFrame({ "LETTER": ["a", "b", "c", "a", "b"], "VALUE" : [10 , 12 , 13, 0, 15] }) def __add_new_col(dt_): dt_['NEW_COL'] = dt_['VALUE'] - dt_['VALUE'].mean() return dt_ pass dt.groupby("LETTER").apply(__add_new_col) LETTER VALUE NEW_COL 0 a 10 5.0 1 b 12 -1.5 2 c 13 0.0 3 a 0 -5.0 4 b 15 1.5 dt LETTER VALUE 0 a 10 1 b 12 2 c 13 3 a 0 4 b 15 In R data.table it is possible by using := operator e.g. dt[, col := ... , by ='LETTER'] I think you can use transform which return Series same length and

Why piping to the same file doesn't work on some platforms?

断了今生、忘了曾经 提交于 2019-11-29 14:09:33
In cygwin, the following code works fine $ cat junk bat bat bat $ cat junk | sort -k1,1 |tr 'b' 'z' > junk $ cat junk zat zat zat But in the linux shell(GNU/Linux), it seems that overwriting doesn't work [41] othershell: cat junk cat cat cat [42] othershell: cat junk |sort -k1,1 |tr 'c' 'z' zat zat zat [43] othershell: cat junk |sort -k1,1 |tr 'c' 'z' > junk [44] othershell: cat junk Both environments run BASH. I am asking this because sometimes after I do text manipulation, because of this caveat, I am forced to make the tmp file. But I know in Perl, you can give "i" flag to overwrite the

Need Perl inplace editing of files not on command line

不想你离开。 提交于 2019-11-29 05:46:10
I have a program that has a number of filenames configured internally. The program edits a bunch of configuration files associated with a database account, and then changes the database password for the database account. The list of configuration files is associated with the name of the database account via an internal list. When I process these files, I have the following loop in my program: BEGIN { $^I = '.oldPW'; } # Enable in-place editing ... foreach (@{$Services{$request}{'files'}}) { my $filename = $Services{$request}{'configDir'} . '/' . $_; print "Processing ${filename}\n"; open

What are the best options for Rich Text Editing in Rails?

隐身守侯 提交于 2019-11-29 00:53:08
问题 I'd like to use Rich Text Editing in place on forms in order to let admins change instructions. What are the best options for doing this? [To be more clear - the admins are non-technical but may want to control some formatting without using markup or with as little markup as possible. What I'd like is for them to be able to edit inline all AJAXy with an RTE featuring some formatting controls and then submit and be able to see what the instructions will look like to the end user without

Separate the alphabet and digit such that their relative order remains the same in O(n) time and O(1) space

狂风中的少年 提交于 2019-11-28 18:55:40
Given an array [a1b7c3d2] convert to [abcd1732] with O(1) space and O(n) time i.e. put the letters on the left and digits on the right such that their relative order is the same. I can think of an O(nlogn) algorithm, but not better. Can somebody please help? AFAIK it can't be done. This is essentially a single step of the RADIX sort algorithm. And AFAIK stable RADIX sort can't be done in-place. edit Wikipedia agrees with me (for what that's worth): http://en.wikipedia.org/wiki/Radix_sort#Stable_MSD_radix_sort_implementations MSD Radix Sort can be implemented as a stable algorithm, but requires

How to remove trailing whitespaces for multiple files?

删除回忆录丶 提交于 2019-11-28 15:57:32
Are there any tools / UNIX single liners which would remove trailing whitespaces for multiple files in-place . E.g. one that could be used in the conjunction with find . You want sed --in-place 's/[[:space:]]\+$//' file That will delete all POSIX standard defined whitespace characters, including vertical tab and form feed. Also, it will only do a replacement if the trailing whitespace actually exists, unlike the other answers that use the zero or more matcher ( * ). --in-place is simply the long form of -i . I prefer to use the long form in scripts because it tends to be more illustrative of

In-Place Quicksort in matlab

风格不统一 提交于 2019-11-28 14:00:00
I wrote a small quicksort implementation in matlab to sort some custom data. Because I am sorting a cell-array and I need the indexes of the sort-order and do not want to restructure the cell-array itself I need my own implementation (maybe there is one available that works, but I did not find it). My current implementation works by partitioning into a left and right array and then passing these arrays to the recursive call. Because I do not know the size of left and and right I just grow them inside a loop which I know is horribly slow in matlab. I know you can do an in place quicksort, but I

Inplace transformation pandas with groupby

让人想犯罪 __ 提交于 2019-11-28 09:45:04
问题 Would it be possible to mutate DataFrame inplace with groupby statement? import pandas as pd dt = pd.DataFrame({ "LETTER": ["a", "b", "c", "a", "b"], "VALUE" : [10 , 12 , 13, 0, 15] }) def __add_new_col(dt_): dt_['NEW_COL'] = dt_['VALUE'] - dt_['VALUE'].mean() return dt_ pass dt.groupby("LETTER").apply(__add_new_col) LETTER VALUE NEW_COL 0 a 10 5.0 1 b 12 -1.5 2 c 13 0.0 3 a 0 -5.0 4 b 15 1.5 dt LETTER VALUE 0 a 10 1 b 12 2 c 13 3 a 0 4 b 15 In R data.table it is possible by using := operator

Is there an in-place equivalent to 'map' in python?

▼魔方 西西 提交于 2019-11-28 07:07:48
问题 I have a list of strings that I need to sanitize. I have a method for sanitizing them, so I could just do: new_list = map(Sanitize, old_list) but I don't need to keep the old list around. This got me wondering if there's an in-place equivalent to map. Easy enough to write a for loop for it (or a custom in-place map method), but is there anything built in? 回答1: The answer is simply: no. Questions of the form "does XXX exist" never tend to get answered directly when the answer is no , so I

Python Math - TypeError: 'NoneType' object is not subscriptable

久未见 提交于 2019-11-28 06:43:17
I'm making a small program for math (no particular reason, just kind of wanted to) and I ran into the error "TypeError: 'NoneType' object is not subscriptable. I have never before seen this error, so I have no idea what it means. import math print("The format you should consider:") print str("value 1a")+str(" + ")+str("value 2")+str(" = ")+str("value 3a ")+str("value 4")+str("\n") print("Do not include the letters in the input, it automatically adds them") v1 = input("Value 1: ") v2 = input("Value 2: ") v3 = input("Value 3: ") v4 = input("Value 4: ") lista = [v1, v3] lista = list.sort(lista) a