import pandas as pd
import numpy as np
idx = pd.IndexSlice
ix = pd.MultiIndex.from_product(
[['2015', '2016', '2017', '2018'],
['2016', '2017', '2018', '2019', '2020'],
['A', 'B', 'C']],
names=['SimulationStart', 'ProjectionPeriod', 'Group']
)
df = pd.DataFrame(np.random.randn(60, 1), index=ix, columns=['Origin'])
origin = df.loc[idx[:, :, :], 'Origin'].values
increase_over_base_percent = 0.3
increase_over_base_abs = 10
abs_level = 1
min_increase = 0.001
'Is there a way to do this comparison without using for loops?'
# The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
change = pd.Series(np.nan)
i = 0
for element in origin:
change[i] = max(
min(element * (1 + increase_over_base_percent),
element + increase_over_base_abs,
abs_level),
element + min_increase)
i += 1
print(change)
# Write results to a new column in the DataFrame ('Change')
df.loc[idx[:, :, :], 'Change'] = change
# Add data on 'Group' level
group_qualifier = [0, 0, 1]
# Is there a way to apply the group_qualifier to the group level without having to slice each index?
# Note: the formula does not work yet (results are to be reported in a new column of the DataFrame)
df.loc[idx[:], 'GroupQA'] = group_qualifier
'This is the part I am struggling with most (my index values are objects, not integers or floats;'
'and the comparison of values within the DataFrame does not work either)'
# Create new column 'Selected'; use origin values for all combinations where
# projectionPeriod < simulationStart & group_qualifier value == 0;
# use change values for all other combinations
values = df.index.get_level_values
mask = (values('ProjectionPeriod') - values('SimulationStart')) <= 1
mask = mask * df.loc[idx[:], 'GroupQA'].values
selected = df.loc[mask]
df.loc[idx[:, :, :], 'Selected'] = selected
通过使用上面的代码,我可以从数据库加载数据并在用户列表中显示给用户,他们可以在这里键入并选择用户。假设我想设置2个默认选择值,例如user_id = 1和2,我如何在选择2中这样做?
答案 0 :(得分:2)
您需要将一组值传递给select2控件然后刷新它。例如,要在页面加载时选择user_id 1和2,您需要执行以下操作。
var user_ids = [1,2];
$('#YourSelect2Control').val(user_ids);
$('#YourSelect2Control').select2();
答案 1 :(得分:2)
答案 2 :(得分:0)
根据http://demos.krajee.com/widget-details/select2的文档:
// Tagging support Multiple
echo '<label class="control-label">Tag Multiple</label>';
echo Select2::widget([
'name' => 'color_1',
'value' => ['red', 'green'], // initial value
'data' => $data,
'options' => ['placeholder' => 'Select a color ...', 'multiple' => true],
'pluginOptions' => [
'tags' => true,
'maximumInputLength' => 10
],
]);
注意&#39;值&#39; key有一个值数组。这将设置多个默认选择&#39;选项。如果不使用&#39;标签,文档不会显示此功能。但是选择。