示例数据框:
df = pd.DataFrame({
'audio': ['audio1', 'audio2', 'audio3'],
'col1': ['A', 'B', 'C'],
'col2': ['D', 'E', 'F']
})
对于这样的行,我需要随机获取列“ audio”的值和col1或col2列的值:
audio value (value is random value from col1 or col2)
audio1 A
audio2 E
audio3 C
答案 0 :(得分:2)
这是一种基于
library(shiny)
library(DT)
server <- function(input, output) {
output$iris_type <- DT::renderDataTable({
datatable(data.frame(Species=paste0("<a href='#filtered_data'",
"alt='",unique(iris$Species),"'",
"onclick=\"",
"tabs = $('.tabbable .nav.nav-tabs li');",
"tabs.each(function() {",
"$(this).removeClass('active')",
"});",
"$(tabs[1]).addClass('active');",
"tabsContents = $('.tabbable .tab-content .tab-pane');",
"tabsContents.each(function() {",
"$(this).removeClass('active')",
"});",
"$(tabsContents[1]).addClass('active');",
"$('#filtered_data').trigger('change').trigger('shown');",
"Shiny.onInputChange('species', getAttribute('alt'));",
"Shiny.onInputChange('dataset', 'Filtered Data');",
"\">",
unique(iris$Species),
"</a>")),
escape = FALSE)
})
output$filtered_data <- DT::renderDataTable({
if(is.null(input$species)){
datatable(iris)
}else{
datatable(iris[iris$Species %in% input$species, ])
}
})
}
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
conditionalPanel(
'input.dataset === "Iris Type"',
helpText("This is the full Iris panel")
),
conditionalPanel(
'input.dataset === "Filtered Data"',
helpText("This is the filtered panel")
),
width = 2
),
mainPanel(
tabsetPanel(
id = 'dataset',
tabPanel("Iris Type", DT::dataTableOutput("iris_type")),
tabPanel("Filtered Data", DT::dataTableOutput("filtered_data"))
)
))
)
shinyApp(ui = ui, server = server)
的方法:
numpy
v = df[['col1', 'col2']].values
ix = np.random.randint(0,v.shape[1],df.shape[0])
df['value'] = np.take_along_axis(v, ix[:,None], 1)
答案 1 :(得分:2)
您还可以尝试使用axis=1
进行采样:
df.set_index('audio').sample(n=1,axis=1)
#df.set_index('audio').apply(np.random.choice,axis=1) #for random values in each row
示例:
i=1
while i < 6:
print(df.set_index('audio').sample(n=1,axis=1),'\n')
i += 1
col2
audio
audio1 D
audio2 E
audio3 F
col2
audio
audio1 D
audio2 E
audio3 F
col1
audio
audio1 A
audio2 B
audio3 C
col1
audio
audio1 A
audio2 B
audio3 C
col2
audio
audio1 D
audio2 E
audio3 F