So I have 3 classes, a group class, a sensor class and a value class. The classes are roughly defined like this:
class SensorGroup(models.Model):
name = models.CharField(max_length=100)
logging_on = models.BooleanField(default=False)
class Sensor(models.Model):
device_id = models.CharField(max_length=25, primary_key=True)
name = models.CharField(max_length=25, blank=False)
last_updated = models.DateTimeField(auto_now=True)
sensor_group = models.ForeignKey(SensorGroup, related_name='sensors')
class Value(models.Model):
sensor = models.ForeignKey(Sensor, related_name='values')
time = models.DateTimeField(auto_now_add=True)
value = models.FloatField(blank=False)
The values are added for every sensor at 20 second intervals. I want to turn it into a table like this using pandas.DataFrame.from_records()
say for group 1:
| sensor_1 | sensor_2 | sensor_3 | timestamp |
|----------+----------+----------+--------------|
| 1.5 | 2.0 | 1.0 | 12:33 2/4/17 |
|----------+----------+----------+--------------|
| 2.0 | 1.5 | 3.3 | 12:34 2/4/17 |
....
What I've tried so far:
qs = Sensors.all().values('name', 'values__value')
df = pd.DataFrame.from_records(list(qs))
print(df)
Yields
name values__value
0 sensor_1 0.0
1 sensor_1 1.0
2 sensor_1 2.0
3 sensor_2 0.0
4 sensor_2 1.0
5 sensor_2 2.0
Is there a way to make this query and convert it into a dataset?
Have a look at pandas pivot_table , if you extract the timestamp as well in the queryset, you could then do something like:
table = pd.pivot_table(df, values='values__value', index=['timestamp'], columns=['name']).reset_index()
to generate the table
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.