multiple-columns
How to access a column in a list of lists in python
List comprehensions are your friend when working with lists of lists: In [111]: alist Out[111]: [[0, 1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 11], [12, 13, 14, 15, 16, 17], [18, 19, 20, 21, 22, 23]] In [112]: [row[1] for row in alist] Out[112]: [1, 7, 13, 19] There’s also a handy … Read more
Splitting multiple columns into rows in pandas dataframe
You can first split columns, create Series by stack and remove whitespaces by strip: s1 = df.value.str.split(‘,’, expand=True).stack().str.strip().reset_index(level=1, drop=True) s2 = df.date.str.split(‘,’, expand=True).stack().str.strip().reset_index(level=1, drop=True) Then concat both Series to df1: df1 = pd.concat([s1,s2], axis=1, keys=[‘value’,’date’]) Remove old columns value and date and join: print (df.drop([‘value’,’date’], axis=1).join(df1).reset_index(drop=True)) ticker account value date 0 aa assets 100 20121231 … Read more
How does cellForRowAtIndexPath work?
I’ll try and break it down (example from documention) /* * The cellForRowAtIndexPath takes for argument the tableView (so if the same object * is delegate for several tableViews it can identify which one is asking for a cell), * and an indexPath which determines which row and section the cell is returned for. */ … Read more
Pandas dataframe – running sum with reset
You can use 2 times cumsum(): # reset val desired_col #0 0 1 1 #1 0 5 6 #2 0 4 10 #3 1 2 2 #4 1 -1 -1 #5 0 6 5 #6 0 4 9 #7 1 2 2 df[‘cumsum’] = df[‘reset’].cumsum() #cumulative sums of groups to column des df[‘des’]= df.groupby([‘cumsum’])[‘val’].cumsum() print … Read more
How to make text over flow into two columns automatically
The good news is that there is a CSS-only solution. If it was implemented, it would look like this: div.multi { column-count: 3 column-gap: 10px; column-rule: 1px solid black; }
How to print third column to last column?
…or a simpler solution: cut -f 3- INPUTFILE just add the correct delimiter (-d) and you got the same effect.
R: Replace multiple values in multiple columns of dataframes with NA
You can also do this using replace: sel <- grepl(“var”,names(df)) df[sel] <- lapply(df[sel], function(x) replace(x,x %in% 3:4, NA) ) df # name foo var1 var2 #1 a 1 1 NA #2 a 2 2 NA #3 a 3 NA NA #4 b 4 NA NA #5 b 5 5 NA #6 b 6 6 NA … Read more
Assign unique ID based on two columns [duplicate]
We can do this in base R without doing any group by operation df$ID <- cumsum(!duplicated(df[1:2])) df # School Student Year ID #1 A 10 1999 1 #2 A 10 2000 1 #3 A 20 1999 2 #4 A 20 2000 2 #5 A 20 2001 2 #6 B 10 1999 3 #7 B 10 … Read more