在多索引熊猫数据框中交换两行

时间:2018-09-21 08:57:23

标签: python pandas swap multi-index

对于简单的表格,如下所示:

df.loc[['b','a'],:]
Out[21]: 
   0  1  2
b  4  5  6
a  1  2  3

我们可以通过调用以下内容交换两行“ a”和“ b”:

df = pd.DataFrame(np.arange(12).reshape((4, 3)),
    index=[['a', 'a', 'b', 'b'], [1, 2, 1, 2]],
    columns=[['Ohio', 'Ohio', 'Colorado'],
    ['Green', 'Red', 'Green']])

     Ohio     Colorado
    Green Red    Green
a 1     0   1        2
  2     3   4        5
b 1     6   7        8
  2     9  10       11

我希望loc()调用对以下多级索引表具有相同的作用:

df.loc[['b','a'],:]

但是当我跑步时:

df.reindex(index=['b', 'a'], level=0)

     Ohio     Colorado
    Green Red    Green
b 1     6   7        8
  2     9  10       11
a 1     0   1        2
  2     3   4        5

表保持不变。

我可以通过调用以下内容来交换行:

df.loc[['b','a'],:]

我的.page-light { background-repeat: no-repeat; background-position: center top; } @media (max-width: 767.98px) { .page-light { background-image: url('/img/hero/front-introduction-mobile.png'); } } @media (min-width: 768.00px) { .page-light { background-image: url('/img/hero/front-introduction-1.png'); -webkit-animation: animation-home-background 9000ms infinite; -moz-animation: animation-home-background 9000ms infinite; -o-animation: animation-home-background 9000ms infinite; animation: animation-home-background 9000ms infinite; -webkit-animation-delay: 1s; /* Chrome, Safari, Opera */ animation-delay: 3s; } } @-webkit-keyframes animation-home-background { 0% { background-image: url('/img/hero/front-introduction-1.png'); } 40% { background-image: url('/img/hero/front-introduction-2.png'); } 80% { background-image: url('/img/hero/front-introduction-3.png'); } 100% { background-image: url('/img/hero/front-introduction-1.png'); } } @keyframes animation-home-background { 0% { background-image: url('/img/hero/front-introduction-1.png'); } 33% { background-image: url('/img/hero/front-introduction-2.png'); } 66% { background-image: url('/img/hero/front-introduction-3.png'); } 100% { background-image: url('/img/hero/front-introduction-1.png'); } } 发生了什么事?

0 个答案:

没有答案