使用特定的索引和过滤从 Pandas df 获取值

橡树

我有以下数据框:

df
                                         eff   inv-cost  fix-cost  var-cost  inst-cap  cap-lo        cap-up  wacc  depreciation  annuity-factor
Site In Site Out Transmission Commodity                                                                                                        
Mid     North    hvac         Elec       0.9  1650000.0       0.0       0.0       0.0     0.0  1.500000e+15  0.07          40.0        0.075009
        South    hvac         Elec       0.9  1650000.0       0.0       0.0       0.0     0.0  1.500000e+15  0.07          40.0        0.075009
North   Mid      hvac         Elec       0.9  1650000.0       0.0       0.0       0.0     0.0  1.500000e+15  0.07          40.0        0.075009
        South    hvac         Elec       0.9  1650000.0       0.0       0.0       0.0     0.0  1.500000e+15  0.07          40.0        0.075009
South   Mid      hvac         Elec       0.9  1650000.0       0.0       0.0       0.0     0.0  1.500000e+15  0.07          40.0        0.075009
        North    hvac         Elec       0.9  1650000.0       0.0       0.0       0.0     0.0  1.500000e+15  0.07          40.0        0.075009

我想将值放入Site InSite Out作为由元组组成的列表。下面是我想要的列表示例:

list = [('Mid','North'),
        ('South', 'Mid'),
        ('South', 'North')]

关键点在这里是从得到的值Site In,并Site Out与大熊猫的功能尽可能地简单,也是因为从发射'Mid''South'与从传输等于'South''Mid',一些列表的创建的元素应该被过滤。

以下是我到目前为止的想法,但也许您可以找到更好的方法?

1) 获取Site Inand 的Site Out并创建一个列表,该列表可能如下所示:

list = [('Mid','North'), ('Mid','South'),
        ('North', 'Mid'), ('North', 'South'),
        ('South', 'Mid'), ('South', 'North')]

2)因为一半的元素是相等的,而不是必要的,例如;('Mid','North')& ('North', 'Mid'),可以删除其中之一。

3)最后,我想要以下任何一项(顺序无关):

list = [('Mid','North'), ('Mid','South'), ('North', 'South')]
list = [('North','Mid'), ('Mid','South'), ('North', 'South')]
list = [('South','Mid'), ('Mid','North'), ('North', 'South')]
etc...

https://github.com/rl-institut/urbs-oemof/blob/dev/mimo.xlsx的df传输表来源

PS:我不知道用哪个pandas函数来获取第一项,也不知道如何弹出第二项中提到的元素。如果你也有更好的算法,我很乐意使用它。

It_is_Chris

我想我明白你在找什么:

对于第 1 步:只需获取索引值:

# reset the index so that 'Site In' and 'Site Out' are left
lis = list(df.reset_index(level=[2,3]).index.values)

[('Mid', 'North'),
 ('Mid', 'South'),
 ('North', 'Mid'),
 ('North', 'South'),
 ('South', 'Mid'),
 ('South', 'North')]

然后使用set一些列表理解:

list(set(tuple(sorted(x)) for x in lis))

[('Mid', 'North'), ('Mid', 'South'), ('North', 'South')]

我假设你的多索引看起来像:

MultiIndex(levels=[['Mid', 'North', 'South'], ['Mid', 'North', 'South'], ['hvac'], ['Elec']],
           labels=[[0, 0, 1, 1, 2, 2], [1, 2, 0, 2, 0, 1], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]],
           names=['Site In', 'Site Out', 'Transmission', 'Commodity'])

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章