How to append a sequential number for every element in a data frame?
Another option is to use match
with the unique
elements of each of the column and the do the paste
df[] <- paste(col(df), sapply(df, function(x) match(x, unique(x))),
as.matrix(df), sep=".")
df
# V1 V2 V3
#1 1.1.a 2.1.c 3.1.i
#2 1.1.a 2.2.d 3.2.j
#3 1.2.b 2.3.e 3.3.k
#4 1.2.b 2.3.e 3.4.l
#5 1.2.b 2.4.f 3.5.m
Or using tidyverse
library(tidyverse)
imap(seq_along(df), ~
df %>%
select(.x) %>%
mutate_at(1, funs(paste(.y, match(., unique(.)), ., sep="." )))) %>%
bind_cols
# V1 V2 V3
#1 1.1.a 2.1.c 3.1.i
#2 1.1.a 2.2.d 3.2.j
#3 1.2.b 2.3.e 3.3.k
#4 1.2.b 2.3.e 3.4.l
#5 1.2.b 2.4.f 3.5.m
Add a sequence number to each element in a group using python
The question is how do I sort on multiple columns of data.
One simple trick is to use the key
parameter to the sorted function.
You'll be sorting by a string built from the columns of the array.
rows = ...# your source data
def date_to_sortable_string(date):
# use datetime package to convert string to sortable date.
pass
# Assume x[0] === patient_id and x[1] === encounter date
# Sort by patient_id and date
rows_sorted = sorted(rows, key=lambda x: "%0.5d-%s" % (x[0], date_to_sortable_string(x[1])))
for row in rows_sorted:
print row
How to append column number in front of every element?
Try this:
df[] <- Map(paste0, seq_along(df), df)
df
## a b
## 1 1a 2b
## 2 1a 2b
How to Add Incremental Numbers to a New Column Using Pandas
Here:
df = df.reset_index()
df = df.rename(columns={"index":"New_ID"})
df['New_ID'] = df.index + 880
In R Shiny how to append to each list element the sequential number of times it appears in the list using sortable js?
A first draft using data.table
:
library(shiny)
library(sortable)
library(htmlwidgets)
library(data.table)
icons <- function(x) {lapply(x,function(x){tags$div(tags$strong(x))})}
ui <- fluidPage(
tags$head(
tags$style(HTML('
#dragTo {list-style-type: none; counter-reset: css-counter 0;}
#dragTo > div {counter-increment: css-counter 1;}
#dragTo > div:before {content: counter(css-counter) ". ";}
')
)
),
div(
style = "margin-top: 2rem; width: 60%; display: grid; grid-template-columns: 1fr 1fr; gap: 2rem; align-items: start;",
div(
div(
class = "panel panel-default",
div(class = "panel-heading", "Drag from here"),
div(
class = "panel-body",
id = "dragFrom",
icons(c("Puppies", "Kittens"))
)
),
),
div(
div(
class = "panel panel-default",
div(class = "panel-heading", "Drag to here"),
div(
class = "panel-body",
id = "dragTo"
)
)
)
),
sortable_js(
"dragFrom",
options = sortable_options(
group = list(
pull = "clone",
name = "group1",
put = FALSE
)
)
),
sortable_js(
"dragTo",
options = sortable_options(
group = list(
group = "group1",
put = TRUE,
pull = TRUE
),
onSort = sortable_js_capture_input(input_id = "selected")
)
),
helpText(h5(strong("Output to table:"))),
tableOutput("table1")
)
server <- function(input, output) {
dragToLabels <- reactive({
# browser()
# DT <- data.table(data = paste0(seq_along(input$selected), ". ", input$selected))
req(input$selected)
DT <- data.table(item = input$selected)
DT[, c("rownumber", "letter") := .(.I, LETTERS[seq_len(.N)]), by = item]
setcolorder(DT, c("rownumber", "item", "letter"))
# DT[, data := paste0(rownumber, ". ", item, " ", letter)][, c("rownumber", "item", "letter") := NULL] # paste to a single column
})
output$table1 <- renderTable({dragToLabels()})
}
shinyApp(ui, server)
How to add secondary sequential numbering to elements dragged in to a hierarchy tree node using jstree?
script <- '
$(document).ready(function(){
var LETTERS = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"];
var Visited = {};
$("#mytree").on("copy_node.jstree", function(e, data){
var oldid = data.original.id;
var visited = Object.keys(Visited);
if(visited.indexOf(oldid) === -1){
Visited[oldid] = 0;
}else{
Visited[oldid]++;
}
var letter = LETTERS[Visited[oldid]];
var node = data.node;
var id = node.id;
var index = $("#"+id).index() + 1;
var text = index + ". " + node.text + " " + letter;
Shiny.setInputValue("choice", text);
var instance = data.new_instance;
instance.rename_node(node, text);
});
});
'
How to append rows in a pandas dataframe in a for loop?
Suppose your data looks like this:
import pandas as pd
import numpy as np
np.random.seed(2015)
df = pd.DataFrame([])
for i in range(5):
data = dict(zip(np.random.choice(10, replace=False, size=5),
np.random.randint(10, size=5)))
data = pd.DataFrame(data.items())
data = data.transpose()
data.columns = data.iloc[0]
data = data.drop(data.index[[0]])
df = df.append(data)
print('{}\n'.format(df))
# 0 0 1 2 3 4 5 6 7 8 9
# 1 6 NaN NaN 8 5 NaN NaN 7 0 NaN
# 1 NaN 9 6 NaN 2 NaN 1 NaN NaN 2
# 1 NaN 2 2 1 2 NaN 1 NaN NaN NaN
# 1 6 NaN 6 NaN 4 4 0 NaN NaN NaN
# 1 NaN 9 NaN 9 NaN 7 1 9 NaN NaN
Then it could be replaced with
np.random.seed(2015)
data = []
for i in range(5):
data.append(dict(zip(np.random.choice(10, replace=False, size=5),
np.random.randint(10, size=5))))
df = pd.DataFrame(data)
print(df)
In other words, do not form a new DataFrame for each row. Instead, collect all the data in a list of dicts, and then call df = pd.DataFrame(data)
once at the end, outside the loop.
Each call to df.append
requires allocating space for a new DataFrame with one extra row, copying all the data from the original DataFrame into the new DataFrame, and then copying data into the new row. All that allocation and copying makes calling df.append
in a loop very inefficient. The time cost of copying grows quadratically with the number of rows. Not only is the call-DataFrame-once code easier to write, its performance will be much better -- the time cost of copying grows linearly with the number of rows.
Related Topics
To Find Most Frequently Occuring Element in Matrix in R
Convert Categorical Variables to Numeric in R
Setting Individual Axis Limits With Facet_Wrap and Scales = "Free" in Ggplot2
Why Are These Numbers Not Equal
Finding All Duplicate Rows, Including "Elements With Smaller Subscripts"
Left Align Two Graph Edges (Ggplot)
Add Column Which Contains Binned Values of a Numeric Column
Error: Unexpected Symbol/Input/String Constant/Numeric Constant/Special in My Code
Plot Multiple Columns on the Same Graph in R
Reorder Levels of a Factor Without Changing Order of Values
How to Convert Only Some Positive Numbers to Negative Numbers (Conditional Recoding)
Find Duplicated Elements With Dplyr
Calculate the Area Under a Curve
How to Sum a Variable by Group
Understanding Exactly When a Data.Table Is a Reference to (Vs a Copy Of) Another Data.Table