Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python分层(元组)行索引——如何选择所有中间行?_Python_Pandas_Dataframe_Indexing_Hierarchical - Fatal编程技术网

Python分层(元组)行索引——如何选择所有中间行?

Python分层(元组)行索引——如何选择所有中间行?,python,pandas,dataframe,indexing,hierarchical,Python,Pandas,Dataframe,Indexing,Hierarchical,考虑以下代码: row1 = [(2,2), (4,4)] row2 = [(5,5)] row3 = [10, 20, 30, 40] row_tuple_list = [] for r1 in row1: for r2 in row2: for r3 in row3: row_tuple_list.append((r1, r2, r3)) row_index = pd.MultiIndex.from_tuples(row_tuple_list

考虑以下代码:

row1 = [(2,2), (4,4)]
row2 = [(5,5)]
row3 = [10, 20, 30, 40]
row_tuple_list = []
for r1 in row1:
    for r2 in row2:
        for r3 in row3:
            row_tuple_list.append((r1, r2, r3))

row_index = pd.MultiIndex.from_tuples(row_tuple_list, names=['row1', 'row2', 'row3'])

col1 = ['f', 'i']
col2 = ['g', 'h']
col_tuple_list = []
for c1 in col1:
    for c2 in col2:
        col_tuple_list.append((c1, c2))

col_index = pd.MultiIndex.from_tuples(col_tuple_list, names=['col1', 'col2'])

df = pd.DataFrame(index=row_index, columns=col_index)
它生成一个数据帧:

col1                  f         i     
col2                  g    h    g    h
row1   row2   row3                    
(2, 2) (5, 5) 10    NaN  NaN  NaN  NaN
              20    NaN  NaN  NaN  NaN
              30    NaN  NaN  NaN  NaN
              40    NaN  NaN  NaN  NaN
(4, 4) (5, 5) 10    NaN  NaN  NaN  NaN
              20    NaN  NaN  NaN  NaN
              30    NaN  NaN  NaN  NaN
              40    NaN  NaN  NaN  NaN
现在,我想设置这个数据帧的各个元素。比如说,

w=(2,2)
x=(5,5)
y=10

df.loc[(w,x,y),('f','g')] = 200 

print(df)
其中:

col1                  f         i     
col2                  g    h    g    h
row1   row2   row3                    
(2, 2) (5, 5) 10    200  NaN  NaN  NaN
              20    NaN  NaN  NaN  NaN
              30    NaN  NaN  NaN  NaN
              40    NaN  NaN  NaN  NaN
(4, 4) (5, 5) 10    NaN  NaN  NaN  NaN
              20    NaN  NaN  NaN  NaN
              30    NaN  NaN  NaN  NaN
              40    NaN  NaN  NaN  NaN
有没有一种方法可以在不显式设置第二行值的情况下执行此操作(因为我知道行1和行2的出现频率相同)

我试过:

df.loc[(w,slice(None),y),('f','g')] =100

失败了。

太好了!看来我不明白斯瑞克在这里干什么。你能解释一下为什么我也需要使用slice(w)吗?另外,如果你对此有任何想法,我将不胜感激!loc[(w,slice(None),y),('f','g')]应该可以工作,但它不适用于您的数据帧。我认为这与索引“(2,2)”周围的括号有关。它可能不知怎么搞砸了熊猫索引。这在不向w添加slice的情况下也应该有效。df.loc[([w],slice(None),y),('f','g')]快速跟进问题:您能告诉我如何在设置了相同数量的值后访问该数量吗?df.loc[([w],slice(None),y),('f','g')]给了我:row1 row2 row3(2,2)(5,5)10100 Name:(f,g),dtype:object而不仅仅是100。
# you need to use slice for w as well. This should work.
df.loc[(slice(w),slice(None),y),('f','g')]

df
Out[208]: 
col1                  f         i     
col2                  g    h    g    h
row1   row2   row3                    
(2, 2) (5, 5) 10    100  NaN  NaN  NaN
              20    NaN  NaN  NaN  NaN
              30    NaN  NaN  NaN  NaN
              40    NaN  NaN  NaN  NaN
(4, 4) (5, 5) 10    NaN  NaN  NaN  NaN
              20    NaN  NaN  NaN  NaN
              30    NaN  NaN  NaN  NaN