data-manipulation
pandas reset_index after groupby.value_counts()
You need parameter name in reset_index, because Series name is same as name of one of levels of MultiIndex: df_grouped.reset_index(name=”count”) Another solution is rename Series name: print (df_grouped.rename(‘count’).reset_index()) A Amt count 0 1 30 4 1 1 20 3 2 1 40 2 3 2 40 3 4 2 10 2 More common solution instead … Read more
Converting String Array to an Integer Array
You could read the entire input line from scanner, then split the line by , then you have a String[], parse each number into int[] with index one to one matching…(assuming valid input and no NumberFormatExceptions) like String line = scanner.nextLine(); String[] numberStrs = line.split(“,”); int[] numbers = new int[numberStrs.length]; for(int i = 0;i < … Read more
Read a CSV from github into R
Try this: library(RCurl) x <- getURL(“https://raw.github.com/aronlindberg/latent_growth_classes/master/LGC_data.csv”) y <- read.csv(text = x) You have two problems: You’re not linking to the “raw” text file, but Github’s display version (visit the URL for https:\raw.github.com….csv to see the difference between the raw version and the display version). https is a problem for R in many cases, so you … Read more
Arranging rows in custom order using dplyr
We can use factor to change the order in a custom way df %>% arrange(factor(Reg, levels = LETTERS[c(3, 1, 2)]), desc(Res), desc(Pop)) # Reg Res Pop #1 C Urban 501638 #2 C Rural 499274 #3 A Urban 500414 #4 A Rural 500501 #5 B Urban 499922 #6 B Rural 500016 Or with match to get … Read more
How do I remove objects from an array in Java?
[If you want some ready-to-use code, please scroll to my “Edit3” (after the cut). The rest is here for posterity.] To flesh out Dustman’s idea: List<String> list = new ArrayList<String>(Arrays.asList(array)); list.removeAll(Arrays.asList(“a”)); array = list.toArray(array); Edit: I’m now using Arrays.asList instead of Collections.singleton: singleton is limited to one entry, whereas the asList approach allows you to … Read more
Removing elements with Array.map in JavaScript
You should use the filter method rather than map unless you want to mutate the items in the array, in addition to filtering. eg. var filteredItems = items.filter(function(item) { return …some condition…; }); [Edit: Of course you could always do sourceArray.filter(…).map(…) to both filter and mutate]
Split delimited strings in a column and insert as new rows [duplicate]
As of Dec 2014, this can be done using the unnest function from Hadley Wickham’s tidyr package (see release notes http://blog.rstudio.org/2014/12/08/tidyr-0-2-0/) > library(tidyr) > library(dplyr) > mydf V1 V2 2 1 a,b,c 3 2 a,c 4 3 b,d 5 4 e,f 6 . . > mydf %>% mutate(V2 = strsplit(as.character(V2), “,”)) %>% unnest(V2) V1 V2 … Read more
How can I access and process nested objects, arrays, or JSON?
Preliminaries JavaScript has only one data type which can contain multiple values: Object. An Array is a special form of object. (Plain) Objects have the form {key: value, key: value, …} Arrays have the form [value, value, …] Both arrays and objects expose a key -> value structure. Keys in an array must be numeric, … Read more