熊猫-选择迄今为止的最低价值

时间:2019-03-02 11:44:33

标签: python pandas

我是熊猫新手。

我有一个数据框,我想按用户分组,然后在其速度列中找到直到该日期的最低分数。

所以我不能只使用df.groupby(['user'])['speed'].transform('min),因为这将给出所有值的最小值,而不仅仅是从当前行到第一行。

我可以用什么来获得所需的东西?

1 个答案:

答案 0 :(得分:0)

没有看到您的数据集,很难直接帮助您。问题确实可以归结为以下内容。您需要选择要使用的数据范围(因此,请为日期范围选择行,为用户/速度选择列)。

看起来像func getAlerts(setCompletion: @escaping (Bool) -> ()) { self.mapView.removeAnnotations(mapView.annotations) MapArray.alertNotificationCoordinatesArray.removeAll() MapArray.userAlertNotificationArray.removeAll() print(" MapArray.alertNotificationCoordinatesArray before getNewerAlerts is: \(MapArray.alertNotificationCoordinatesArray)") print(" self.userAlertNotificationArray before getNewerAlerts is: \(MapArray.userAlertNotificationArray)") ref = Database.database().reference() // ref?.child("Continent").child("Europe").child("Country").child("Italy").child("Region").child("Emilia-Romagna").child("City").child("Bologna").child("Community").child("Alert Notifications").observe(.childAdded, with: { (snapshot) in ref?.child("Continent").child("Europe").child("Country").child("Italy").child("Region").child("Emilia-Romagna").child("City").child("Bologna").child("Community").child("Alert Notifications").observe(DataEventType.childAdded, with: { (snapshot) in // self.mapView.removeAnnotations(self.mapView.annotations) // print(" added snapshot is: \(snapshot)") guard let data = snapshot.value as? [String:String] else { return } // guard let firebaseKey = snapshot.key as? String else { return } let firebaseKey = snapshot.key // let date = data!["Date"] // let time = data!["Time"] let dataLatitude = data["Latitude"]! let dataLongitude = data["Longitude"]! let type = data["Description"]! let id = Int(data["Id"]!) let userName = data["user"]! let doubledLatitude = Double(dataLatitude) let doubledLongitude = Double(dataLongitude) let recombinedCoordinate = CLLocationCoordinate2D(latitude: doubledLatitude!, longitude: doubledLongitude!) let userAlertAnnotation = UserAlert(type: type, coordinate: recombinedCoordinate, firebaseKey: firebaseKey, title: type,id: id!, userName: userName) MapArray.userAlertNotificationArray.append(userAlertAnnotation) // array of notifications coming from Firebase MapArray.alertNotificationCoordinatesArray.append(recombinedCoordinate) // array for checkig alerts on route print(" MapArray.alertNotificationCoordinatesArray after getNewerAlerts is: \(MapArray.alertNotificationCoordinatesArray)") print(" self.userAlertNotificationArray after getNewerAlerts is: \(MapArray.userAlertNotificationArray)") setCompletion(true) self.mapView.addAnnotations(MapArray.userAlertNotificationArray) }) // ref?.child("Continent").child("Europe").child("Country").child("Italy").child("Region").child("Emilia-Romagna").child("City").child("Bologna").child("Community").child("Alert Notifications").observe(.childRemoved, with: { (snapshot) in ref?.child("Continent").child("Europe").child("Country").child("Italy").child("Region").child("Emilia-Romagna").child("City").child("Bologna").child("Community").child("Alert Notifications").observe(DataEventType.childRemoved, with: { (snapshot) in print(" self.userAlertNotificationArray before getDeletedAlerts snapshot is: \(MapArray.userAlertNotificationArray)") print(" MapArray.alertNotificationCoordinatesArray before getDeletedAlerts snapshot is: \(MapArray.alertNotificationCoordinatesArray)") print(" removed snapshot is: \(snapshot)") guard let data = snapshot.value as? [String:String] else { return } let firebaseKey = snapshot.key // let date = data!["Date"] // let time = data!["Time"] let dataLatitude = data["Latitude"]! let dataLongitude = data["Longitude"]! let type = data["Description"]! let id = Int(data["Id"]!) let userName = data["user"]! let doubledLatitude = Double(dataLatitude) let doubledLongitude = Double(dataLongitude) let recombinedCoordinate = CLLocationCoordinate2D(latitude: doubledLatitude!, longitude: doubledLongitude!) _ = UserAlert(type: type, coordinate: recombinedCoordinate, firebaseKey: firebaseKey, title: type,id: id!, userName: userName) MapArray.userAlertNotificationArray.removeAll(where: { ($0.firebaseKey == firebaseKey) }) //remove the alert MapArray.alertNotificationCoordinatesArray.removeAll(where: { ($0.latitude == recombinedCoordinate.latitude && $0.longitude == recombinedCoordinate.longitude) }) self.mapView.removeAnnotations(self.mapView.annotations) self.mapView.addAnnotations(MapArray.userAlertNotificationArray) print(" self.userAlertNotificationArray after getDeletedAlerts snapshot is: \(MapArray.userAlertNotificationArray)") print(" MapArray.alertNotificationCoordinatesArray after getDeletedAlerts snapshot is: \(MapArray.alertNotificationCoordinatesArray)") setCompletion(true) }) }
从那里,您可以对值执行简单的x = df.loc[["2-4-2018","2-4-2019"], ['users', 'speed']]或对值的索引执行x['users'].min()
我还没有玩过Dataframe,但是您正在寻找如何切片 Dataframe。