简体   繁体   English

将值从一个 dataframe 切片复制到另一个:使用“IndexSlice”的多索引 pandas 数据帧的切片是否总是一致地排序?

[英]Copying values from one dataframe slice to another: are slices from multi-indexed pandas dataframes using `IndexSlice` always ordered consistently?

Context语境

Say I have a multi-indexed dataframe as follows:假设我有一个多索引 dataframe 如下:

import numpy as np
import pandas as pd

arrays = [
    ["bar", "bar", "baz", "baz", "foo", "foo", "qux", "qux"],
    ["one", "two", "one", "two", "one", "two", "one", "two"],
]
tuples = list(zip(*arrays))
index = pd.MultiIndex.from_tuples(tuples, names=["first", "second"])
data = np.array([
    [1, 2],
    [3, 4],
    [5, 6],
    [7, 8],
    [9, 10],
    [11, 12],
    [13, 14],
    [15, 16],
])
df = pd.DataFrame(data, index=index, columns=('a', 'b'))

which looks something like this:看起来像这样:

               a   b
first second        
bar   one      1   2
      two      3   4
baz   one      5   6
      two      7   8
foo   one      9  10
      two     11  12
qux   one     13  14
      two     15  16

I would like to copy the values of column a for the first index level bar into the same column for the first index level qux , aligned on the second level of the index (here called second ).我想将第一个索引级别bar的列a的值复制到第一个索引级别qux的同一列中,在索引的第二个级别(这里称为second )上对齐。 In other words, I would like to obtain the following dataframe from the one above:换句话说,我想从上面的一个中获得以下 dataframe :

               a   b
first second        
bar   one      1   2
      two      3   4
baz   one      5   6
      two      7   8
foo   one      9  10
      two     11  12
qux   one      1  14  # <-- column a changed to match first = bar for second = one
      two      3  16  # <-- column a changed to match first = bar for second = two

I understand based on the answer given to this question I can accomplish this by using pd.IndexSlice in conjunction with .loc and .values as follows:我理解基于对这个问题的回答,我可以通过将pd.IndexSlice.loc.values结合使用来完成此操作,如下所示:

df.loc[pd.IndexSlice['qux', :], 'a'] = df.loc[pd.IndexSlice['bar', :], 'a'].values

I don't intuitively like this (perhaps/probably unjustifiably) as it's not immediately clear to me if the values will always be aligned on the second index level or not:我不直观地喜欢这个(也许/可能是不合理的),因为我不清楚这些值是否总是在第二个索引级别上对齐:

Question:问题:

Can I guarantee that the above assignment (accessing using .values ) will always be aligned on the second level of the multi-index?我可以保证上述分配(使用.values访问)将始终在多索引的第二级对齐吗?

If not, is there a way of accomplishing what I'm trying to achieve?如果没有,有没有办法实现我想要实现的目标?

No, it will not be aligned, because by using .value (which, by the way, is deprecated in favor of .to_numpy() ), which returns the underlying numpy array, you remove all index/column information, so alignment is not possible.不,它不会对齐,因为通过使用.value (顺便说一下,不推荐使用.to_numpy() ),它返回底层 numpy 数组,删除所有索引/列信息,所以 alignment 不是可能的。

Here's one solution to preserve the alignment:这是保留 alignment 的一种解决方案:

df.loc['qux', 'a'] = df.loc['qux', 'a'].index.map(df.loc['bar', 'a'].to_dict())```

Output: Output:

>>> df
                 a   b
first second          
bar   two      1.0   2
      one      3.0   4
baz   one      5.0   6
      two      7.0   8
foo   one      9.0  10
      two     11.0  12
qux   one      3.0  14
      two      1.0  16

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM