我有以下熊猫df:
df
price max maxperhour
Site Commodity Type
Mid Biomass Stock 6.0 1.500000e+15 1.500000e+15
CO2 Env 0.0 1.500000e+15 1.500000e+15
Coal Stock 7.0 1.500000e+15 1.500000e+15
Elec Demand NaN NaN NaN
Gas Stock 27.0 1.500000e+15 1.500000e+15
Hydro SupIm NaN NaN NaN
Lignite Stock 4.0 1.500000e+15 1.500000e+15
Solar SupIm NaN NaN NaN
Wind SupIm NaN NaN NaN
我想过滤上述df,并在Commodity
和Site == 'Mid'
时创建一个Type == ('Stock' or 'Demand')
项目的列表作为列表。
,因此应使用一些熊猫过滤功能创建以下列表:
df.somefunction()
['Biomass', 'Coal', 'Gas', 'Lignite', 'Elec']
我将如何实现?
最后,如果可能的话,我想将'Elec'
作为最后一个元素,我的意思是;创建列表时,'Elec'
可能是列表的第三个元素,例如:
['Biomass', 'Coal', 'Elec', 'Gas', 'Lignite']
但是,最好将'Elec'
作为最后一个元素,例如:
['Biomass', 'Coal', 'Gas', 'Lignite', 'Elec']
因为它是唯一带有Type == 'Demand'
的元素
来自@jezrael
df[(df.index.get_level_values('Site') == 'Mid') & (df.index.get_level_values('Type') == 'Stock')].index.remove_unused_levels().get_level_values('Commodity').tolist()
答案 0 :(得分:1)
使用var io = require('./io');
io.on('connection', function (socket) {
socket.emit('news', {hello: 'world'});
socket.on('news', function (data) {
console.log(data);
// Depends on your business logic
// Sends to sender-client only
// socket.emit('news', data);
// Sends to all clients except sender
// socket.broadcast.emit('news', data);
// Sends to all client including sender
io.emmit('news', data);
});
});
io.on('error', function () {
console.log("errr");
});
的解决方案:
MultiIndex
替代列:
m1 = (df.index.get_level_values('Site') == 'Mid')
m2 = (df.index.get_level_values('Type') == 'Stock')
m3 = (df.index.get_level_values('Type') == 'Demand')
idx1 = df[m1 & m2].index.remove_unused_levels().get_level_values('Commodity')
idx2 = df[m1 & m3].index.remove_unused_levels().get_level_values('Commodity')
idx = idx1.append(idx2)
print (idx)
Index(['Biomass', 'Coal', 'Gas', 'Lignite', 'Elec'], dtype='object', name='Commodity')