python - 在 pandas MultiIndex 系列中设置值

标签 python pandas indexing series multi-index

我正在尝试以多索引值将一个系列设置为另一个系列。如果没有复杂的技巧,我找不到在 Pandas 中做到这一点的方法。

我的原创系列:

one  1    0.522764
     3    0.362663
     7    0.963108
two  2    0.717855
     4    0.004645
     5    0.077471

我要连接的数据,在级别:

2    0.8
7    0.9
8    0.7

期望的输出:

one    1    0.522764
       3    0.362663
       7    0.963108
two    2    0.717855
       4    0.004645
       5    0.077471
three  2    0.800000
       7    0.900000
       8    0.700000

我想不出一种优雅的方式在 Pandas 中做到这一点。我所能做的就是以下技巧:

# imports
import numpy as np
import pandas as pd 

# to replicate the Series: 
np.arrays = [['one','one','one','two','two','two'],[1,3,7,2,4,5]]
my_series = pd.Series([np.random.random() for i in range(6)],
               index=pd.MultiIndex.from_tuples(list(zip(*np.arrays))))

# the new data I need to add: 
new_data = pd.Series({1: .9, 2: .7, 3: .8})

这是我目前解决它的方法:

# rename the index so that I can call it later 
new_data.index.name = 'level_1' 

# turn it into temporary a dataframe so that I can add a new column 
temp = pd.DataFrame(new_data) 

# create a new column with the desired name for first index level 
temp['level_0'] = 'three'   

# reset index, set the new index, turn into Series again
temp = temp.reset_index().set_index(['level_0', 'level_1'])[0]                              

# append it to the larger dataframe 
my_series = my_series.append(temp)                  

这会产生所需的输出。

问题:在 Pandas 中是否有一种简单、优雅的方法来做到这一点?

最佳答案

选项 1

pd.concat 是一种通过使用 keys 参数在索引或列级别前添加的简便方法。将其与第二个 pd.concat 结合以完成工作。

pd.concat([my_series, pd.concat([new_data], keys=['Three'])])

one    1    0.943246
       3    0.412200
       7    0.379641
two    2    0.883960
       4    0.182983
       5    0.773227
Three  1    0.900000
       2    0.700000
       3    0.800000
dtype: float64

选项 2
或者我们可以构造一个新系列,同时将一个额外的数组插入到 index 参数中。再次使用 pd.concat 进行合并。 注意 我本可以使用pd.MultiIndex.from_arrays,但通过将数组直接传递给index 参数来简化语法。

pd.concat([
    my_series,
    pd.Series(new_data.values, [['Three'] * new_data.size, new_data.index])
])

one    1    0.943246
       3    0.412200
       7    0.379641
two    2    0.883960
       4    0.182983
       5    0.773227
Three  1    0.900000
       2    0.700000
       3    0.800000
dtype: float64

选项 3
另一种使用多索引重建序列的方法。这个使用 pd.MultiIndex.from_product

pd.concat([
    my_series,
    pd.Series(new_data.values, pd.MultiIndex.from_product([['Three'], new_data.index]))
])

one    1    0.943246
       3    0.412200
       7    0.379641
two    2    0.883960
       4    0.182983
       5    0.773227
Three  1    0.900000
       2    0.700000
       3    0.800000
dtype: float64

关于python - 在 pandas MultiIndex 系列中设置值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45861041/

相关文章:

python - 从值数组中过滤 2D numpy 数组

python - 将特定字符串转换为 Pandas 中的数值

MySQL 优化具有大量评分的表

python - 使用具有冗余元素的 python 数组索引 python 数组

python - 如何使用 GAE 的 Python API 将原始字节写入谷歌云存储

python - O(n) 列表减法

python - random.random 到底在做什么

python - 从几个嵌套字典创建一个 panda 数据框

python - 按连续索引号分组

c - 如何在调用之间的 Ruby 扩展中将 C 对象保留在内存中?