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

时间:2017-04-29 16:57:26

标签: python pandas dataframe indexing hierarchical

请考虑以下代码:

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

现在,我想设置此数据框的各个元素。例如,

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

给出:

const {app, Tray, Menu, BrowserWindow} = require('electron'); 
//electron application stuff
const path = require('path'); //allows for use of path
const url = require('url'); //allows for loadURL and url.format
const iconPath = path.join(__dirname, 'icon.png'); //grab the icon
let tray = null; //set the tray to null
let win = null; //set the main window to null

app.on('ready', function(){
 win = new BrowserWindow({width: 600, height: 400, resizable: false}); 
 //create main window

 win.setMenu(null); //the main window had no menu
 win.loadURL(url.format({    //loads the webpage for the main window
 pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true
}))
win.openDevTools(); //starts the application with developer tools open

win.on('minimize',function(event){ //prevents standard minimize 
function of a main window
    event.preventDefault()
        win.hide();
});
win.on('close', function (event) { //prevents the closing of the 
aplication when the window closes
    if( !app.isQuiting){
        event.preventDefault()
        win.hide();
    }
    return false;
});
tray = new Tray(iconPath); //create a new tray
var contextMenu = Menu.buildFromTemplate([  //start buliding out the 
menu for the tray

      { label: 'Insomnia', click:  function(){  //makes the main window reappear
          win.show();
      } },
      { label: 'About', click:  function(){ //shows the about window
        abt = new BrowserWindow({width: 400, height: 400, resizable: false});
        abt.setMenu(null); //the about window has no menu
        abt.loadURL(url.format({  //loads the webpage for the about window
          pathname: path.join(__dirname, 'about.html'),
          protocol: 'file:',
          slashes: true
        }))

      } },
      {
        label: 'Preferences', click:  function(){ //shows the about window
          pref = new BrowserWindow({width: 400, height: 400, resizable: false});
          pref.setMenu(null); //the about window has no menu
          pref.loadURL(url.format({  //loads the webpage for the about window
            pathname: path.join(__dirname, 'preferences.html'),
            protocol: 'file:',
            slashes: true
          }))

        }
      },
      { label: 'Quit', click:  function(){ //quit the application
          app.isQuiting = true;
          app.quit(); //quit called

      } }
  ]);
tray.setToolTip('Insomnia'); //Honestly no clue but itll make the tray 
say insomnia in some other place
tray.setContextMenu(contextMenu); //attach the menu to the tray
});

有没有办法在没有明确设置第二行值的情况下执行此操作(因为我知道row1和row2以相同的频率出现)?

我试过了:

<!DOCTYPE html>
<html>
   <head>
      <meta charset="UTF-8">
      <title>Preferences</title>
   </head>
   <body BGCOLOR="#02D9CC" >
      </div>
      <center> <img src="sleep.png" alt="sleep" style="width:100px;height:100px;"> </center>
      <div style="position:absolute;bottom:250px;margin-left:110px">
      <center>
        <input onclick="storeTimePreference()" type="radio" id="timeType" name="timeType" value="Military"> Military </input>
        <input onclick="storeTimePreference()" type="radio" id ="timeType2" name="timeType" value="Standard" checked="checked"> Standard </input>
      </center>
    </div>
    <div style="position:absolute;bottom:150px;margin-left:110px">
      <center><input type="time" id="defaultTime" /><br>
         <button onlick="readFile()" >Set Default Wake Up Time</button>
      </center>
    </div>
      <div class ="version" style="position:absolute;bottom:5px;color:white;margin-left:225px">
      <center >
        Insomnia Version 1.0.0

      </center>
    </div>
    <script src="./js/script.js"></script>
   </body>
</html>

失败了。

1 个答案:

答案 0 :(得分:1)

.fourthcontainer