我有一个类似的数据框
df = pd.DataFrame(
pd.np.random.randint(1, 10, (20, 2)),
index=["a", "b", "c", "d"] * 5,
columns=["foo", "bar"],
)
看起来像这样
我正在尝试将其转换为以下多索引形式:4个轴标签中的每个标签都有两列,每个列均带有一个“ foo”和“ bar”子列:
我尝试摆弄df.groupby(df.index)
,但后来我不知道如何将这些组变成列。
答案 0 :(得分:2)
将GroupBy.cumcount
用于DataFrame.set_index
的索引中的if($order == '' ) {
$order= id;
}
$homework6 = $mysqli->query("SELECT * FROM homework6 WHERE manufacturer_hebrew LIKE '%$x%' order by $order asc");
$rows = $homework6->fetch_all(MYSQLI_ASSOC);
echo '<div >';
echo '<table border= "1" >';
echo '<th colspan="1">'. '<a href="?order=id">x</a>' .'</th>';
echo '<th colspan="1">'. '<a href="?order=manufacturer_hebrew">x</a>' .'</th>';
echo '<th colspan="1">'. '<a href="?order=manufacturer_english">x</a>' .'</th>';
echo '<th colspan="1">'. '<a href="?order=Models_number">x</a>' .'</th>';
echo '<th colspan="1">'. '<a href="?order=made_in">x</a>' .'</th>';
foreach ($rows as $paz)
{ if ($paz['Models_number'] < 2 ) {
$color = "bg-danger";
}
elseif ($paz['Models_number'] < 10){
$color = "bg-warning";
}
elseif ($paz['Models_number'] < 15){
$color = "bg-success";
}
else {$color = "bg-primary";}
echo '<tr class='.$color.'>';
echo '<td >' .$paz['id']. '</td>';
echo '<td>' .$paz['manufacturer_hebrew']. '</td>';
echo '<td>' .$paz['manufacturer_english']. '</td>';
echo '<td>' .$paz['Models_number']. '</td>';
echo '<td >' .$paz['made_in']. '</td>';
echo '</tr>';
}
echo '</table>';
echo '</div>';
,然后通过DataFrame.unstack
进行整形,并使用DataFrame.swaplevel
来更改DataFrame.sort_index
的列中MultiIndex
的顺序Boost Yap:
Multiindex