请考虑以下数据框:
m.transmission
eff inv-cost fix-cost var-cost
Site In Site Out Transmission Commodity
Mid North hvac Elec 0.90 1650000 16500 0
Mid South hvac Elec 0.90 1650000 16500 0
North Mid hvac Elec 0.90 1650000 16500 0
North South hvac Elec 0.85 3000000 30000 0
South Mid hvac Elec 0.90 1650000 16500 0
South North hvac Elec 0.85 3000000 30000 0
我想根据Site In == 'Mid'
或Site Out == 'Mid'
我该怎么办?在说什么之前,这不是理想的结果:
m.transmission.loc[['Mid']]
Site In Site Out Transmission Commodity
Mid North hvac Elec
Mid South hvac Elec
因为它只会过滤Site In == 'Mid'
期望的输出将是(ofc与列;例如eff,inv-cost,fix-cost,var-cost):
Site In Site Out Transmission Commodity
Mid North hvac Elec
Mid South hvac Elec
North Mid hvac Elec
South Mid hvac Elec
EXTRA
(Pdb) m.transmission.columns
Index(['eff', 'inv-cost', 'fix-cost', 'var-cost', 'inst-cap', 'cap-lo',
'cap-up', 'wacc', 'depreciation'],
dtype='object')
(Pdb) m.transmission.index
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'])
答案 0 :(得分:3)
如果'列'在索引中。
In [217]: df.loc[(df.index.get_level_values('Site In') == 'Mid') |
(df.index.get_level_values('Site Out') == 'Mid')]
Out[217]:
v
Site In Site Out Transmission Commodity
Mid North hvac Elec 1
South hvac Elec 1
North Mid hvac Elec 1
South Mid hvac Elec 1
如果它是一个扁平的数据帧,请使用
In [168]: df.loc[(df['Site In'] == 'Mid') | (df['Site Out'] == 'Mid')]
Out[168]:
Site In Site Out Transmission Commodity
0 Mid North hvac Elec
1 Mid South hvac Elec
2 North Mid hvac Elec
4 South Mid hvac Elec
或者
In [169]: df.loc[df['Site In'].eq('Mid') | df['Site Out'].eq('Mid')]
Out[169]:
Site In Site Out Transmission Commodity
0 Mid North hvac Elec
1 Mid South hvac Elec
2 North Mid hvac Elec
4 South Mid hvac Elec
答案 1 :(得分:1)
<强>更新强>
演示:
In [94]: df
Out[94]:
val
Site_In Site_Out Transmission Commodity
Mid North hvac Elec 1
South hvac Elec 2
North Mid hvac Elec 3
South hvac Elec 4
South Mid hvac Elec 5
North hvac Elec 6
In [95]: df.query("Site_In == 'Mid' or Site_Out == 'Mid'")
Out[95]:
val
Site_In Site_Out Transmission Commodity
Mid North hvac Elec 1
South hvac Elec 2
North Mid hvac Elec 3
South Mid hvac Elec 5
注意:此方法仅适用于不包含空格的索引/列名称
答案 2 :(得分:0)
你可以试试这个......这很容易实现。
In [12]: df[(df["Site In"]=="Mid")|(df["Site Out"]=="Mid")]
Out[12]:
Site In Site Out Transmission Commodity
0 Mid North hvac Elec
1 Mid South hvac Elec
2 North Mid hvac Elec
4 South Mid hvac Elec