使用系列执行两个单独的操作时保持索引完整

时间:2018-04-10 19:13:18

标签: python pandas

我正在尝试创建一个布尔系列,其中我以两种不同的方式处理数据。我正试图找到局部最小值来开始一个布尔计算,在此之前的任何事情,我想返回False。我的问题是我能想到的唯一方法就是将结果系列分为两部分,一部分从组的开始到最小的一部分,一部分从最小到最后一组,最后将它们连接起来。您可以在下面看到我创建了一个False条目列表,然后将其与我从最小开始创建的布尔序列连接起来。这实际上是kludge-y,它不会保持索引的完整性。

ser = pd.concat([pd.Series([False] * (argrelextrema(group['B'].values, np.less)[0][:1][0])), (group[argrelextrema(group['B'].values, np.less)[0][:1][0]:].B.diff().shift(-1) <= -1)])

由此:

       B
5876   500.2
5877   500.3
5878   500.4
5879   498.3
5880   499.0
5881   512  
... 

我最终得到了这样的东西,例如:

1      False
2      False
3      False
5879   True
5880   False
5881   False
... 

为了解决这个问题,我想我可以从组中的第一个开始重置索引,但这似乎更像是kludge-y。

ser.index = np.arange(group.index[0], len(ser))

是否有一种更优雅的方法可以在最小值之前返回False并将其与我创建的布尔系列相结合,保持所有索引的完整性?

1 个答案:

答案 0 :(得分:0)

您可以使用运算符// pre-coded users var existingUsers = [ { firstname: "Gerry", lastname: "Agnew", username: "GerryA", password: "password123", email: "gerry@gmit.ie", phone:"0833333333" }, { firstname: "Sean", lastname: "Duignan", username: "SeanD", password: "password456", email: "sean@gmit.ie", phone:"0822222222" }, { firstname: "Michael", lastname: "Duignan", username: "MichaelD", password: "password789", email: "michael@gmit.ie", phone:"0844444444" } ] //function setup() //{ // setup functions //} function dropdown() { document.getElementById("myDropDwn").classList.toggle("show"); } //========================================================================= // Validation Events username.addEventListener('blur', logUserVerify, true); password.addEventListener('blur', logPassVerify, true); newFName.addEventListener('blur', fNameVerify, true); newLName.addEventListener('blur', lNameVerify, true); newUName.addEventListener('blur', regUserVerify, true); newPass.addEventListener('blur', regPassVerify, true); newEmail.addEventListener('blur', emailVerify, true); newPhone.addEventListener('blur', phoneVerify, true); //========================================================================= // Validation error functions function loginValidate() { if(username == "") { document.getElementById("login-user-error").innerHTML = "Username required"; username.focus(); return false; } if(password == "") { document.getElementById("login-pass-error").innerHTML = "Password required"; password.focus(); return false; } } function registerValidate() { if(registerFName == "") { document.getElementById("reg-FName-error").innerHTML = "First Name required"; newFName.focus(); return false; } if(registerLName == "") { document.getElementById("reg-LName-error").innerHTML = "Last Name required"; newLName.focus(); return false; } if(registerUName == "") { document.getElementById("reg-UName-error").innerHTML = "Username required"; newUName.focus(); return false; } if(registerUName.length < 8) { document.getElementById("reg-UName-error").innerHTML = "Username must be 8 characters or more"; newUName.focus(); return false; } if(registerPass == "") { document.getElementById("reg-pass-error").innerHTML = "Password required"; newPass.focus(); return false; } if(registerEmail == "") { document.getElementById("reg-email-error").innerHTML = "Email required"; newEmail.focus(); return false; } if(registerPhone == "") { document.getElementById("reg-phone-error").innerHTML = "Phone number required"; newPhone.focus(); return false; } } // end of Validation error functions //================================================================================== // Event Functions function logUserVerify() { if (username != "") { document.getElementById("login-user-error").innerHTML = ""; return true; } } function logPassVerify() { if (password != "") { document.getElementById("login-pass-error").innerHTML = ""; return true; } } function fNameVerify() { if (registerFName != "") { document.getElementById("login-FName-error").innerHTML = ""; return true; } } function lNameVerify() { if (registerLName != "") { document.getElementById("login-LName-error").innerHTML = ""; return true; } } function regUserVerify() { if (registerUName != "") { document.getElementById("login-UName-error").innerHTML = ""; return true; } if (registerUName > 8) { document.getElementById("login-UName-error").innerHTML = ""; return true; } } function regPassVerify() { if (registerPass != "") { document.getElementById("login-pass-error").innerHTML = ""; return true; } } function phoneVerify() { if (registerPhone != "") { document.getElementById("login-phone-error").innerHTML = ""; return true; } } function emailVerify() { if (registerEmail != "") { document.getElementById("login-email-error").innerHTML = ""; return true; } } // end of event functions //=================================================================================== // Login/Register Functions function loginUser() { var username = document.getElementById("username").value; var password = document.getElementById("password").value; var userCheck = false; var passCheck = false; loginValidate(); for (i = 0; i < existingUsers.length; i++) { if(username == existingUsers[i].username) { document.getElementById("login-user-error").innerHTML = ""; userCheck = true; } else if(username != existingUsers[i].username) { document.getElementById("login-user-error").innerHTML = "Invalid, user does not exist"; username.focus(); return false; } if(password == existingUsers[i].password) { document.getElementById("login-pass-error").innerHTML = ""; passCheck = true; } else if(password != existingUsers[i].password) { document.getElementById("login-pass-error").innerHTML = "Error, incorrect password"; password.focus(); return false; } if(userCheck == true && passCheck == true) { document.getElementById("loggedIn-Out").innerHTML = username; localStorage.user = username; localStorage.pass = password; return; } } } function logoutUser() { localStorage.removeItem("user"); localStorage.removeItem("pass"); document.getElementById("loggedIn-Out").innerHTML = "Login/Register"; } function registerUser() { var registerFName = document.getElementById("newFName").value; var registerLName = document.getElementById("newLName").value; var registerUName = document.getElementById("newUName").value; var registerPass = document.getElementById("newPass").value; var registerEmail = document.getElementById("newEmail").value; var registerPhone = document.getElementById("newPhone").value; registerValidate(); localStorage.regFname = registerFName; localStorage.regLname = registerLName; localStorage.regUname = registerUName; localStorage.regPass = registerPass; localStorage.regMail = registerEmail; localStorage.regPhone = registerPhone; var newUser = { firstname: registerFName, lastname: registerLName, username: registerUName, password: registerPass, email: registerEmail, phone: registerPhone } for(i = 0; i < existingUsers.length; i++) { if(registerUName == existingUsers[i].username) { document.getElementById("reg-UName-error").innerHTML = "Username already exists"; newUName.focus(); return false; } if(registerEmail == existingUsers[i].email) { document.getElementById("reg-email-error").innerHTML = "Email already exists"; newEmail.focus(); return false; } } existingUsers.push(newUser); } // end of Login/Register Functions //================================================================================ // Password Strength functions function passwordStr() { var passValue = document.getElementById("newPass").value; if(passValue.length >= 8 && passValue.length <= 10) { document.getElementById("passStrength").innerHTML = "Weak"; } else if(passValue.length > 10 && passValue.length <= 16) { document.getElementById("passStrength").innerHTML = "Average"; document.getElementById("passStrength").style.color = "yellow"; } else if(passValue.length > 16) { document.getElementById("passStrength").innerHTML = "Strong"; document.getElementById("passStrength").style.color = "green"; } else { document.getElementById("passStrength").style.color = "red"; } } var passInput = document.getElementById("newPass"); passInput.addEventListener("input", passwordStr); 创建一个布尔系列而不是连接两个系列。换句话说,您的布尔系列应满足两个条件:

项目索引大于或等于最小值索引

项目满足您的其他布尔计算(无论位置如何,您都可以计算所有项目)

&

如您所见,位于最小值之前的项目始终指定为False,而其他项目则指定您计算的布尔值。