Попробуем это, используя stack
, to_frame
и T:
df.index = df.index + 1
df_out = df.stack()
df_out.index = df_out.index.map('{0[1]}_{0[0]}'.format)
df_out.to_frame().T
Выход:
A_1 B_1 C_1 D_1 E_1 A_2 B_2 C_2 D_2 E_2 A_3 B_3 C_3 D_3 E_3
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 5
Поскольку написанное вами то же самое, что:
echo (true ? 1 : true) ? 2 : 3;
, и, как вы знаете, 1 оценивается как true
.
Что вы ожидаете:
echo (true) ? 1 : (true ? 2 : 3);
Поэтому всегда используйте фигурные скобки, чтобы избежать таких путаниц.
Как уже было написано, тернарные выражения остаются ассоциативными в PHP. Это означает, что сначала будет выполнен первый из left , затем второй и т. Д.
Использовать круглые скобки при сомнении.
Тернарный оператор в PHP лево-ассоциативный в отличие от других языков и не работает должным образом.
Разделить второе тернарное предложение с круглыми скобками.
echo true ? 1 : (true ? 2 : 3);
из docs
Example #3 Non-obvious Ternary Behaviour
<?php
// on first glance, the following appears to output 'true'
echo (true?'true':false?'t':'f');
// however, the actual output of the above is 't'
// this is because ternary expressions are evaluated from left to right
// the following is a more obvious version of the same code as above
echo ((true ? 'true' : false) ? 't' : 'f');
// here, you can see that the first expression is evaluated to 'true', which
// in turn evaluates to (bool)true, thus returning the true branch of the
// second ternary expression.
?>