-1

I have imported CSV file with temperature values to Python pandas DataFrame. I would like to find the range where the temperature is between 9 to 11 degrees (Celsius). How to do that?

So far I import CSV to MS Excel and write down the range number, for example 9 to 11 degrees are in cells A19:A34. Then in Python:

df.loc[19:34, 'Temperature'].mean()

The order must be preserved, because I am calibrating temperature sensor and later need to find the average of last 25% of measured values. Let's say in certain condition I measure 10 minutes, the sensor will stabilize, and to get the mean temperature I find the average of last measured values.

Later I follow my previous topic: Find the average of last 25% from the input range in pandas

fred
  • 93
  • 8

1 Answers1

0

With the hint of ddejohn I solved my problem. Here is the working code.

import pandas as pd
df = pd.read_csv('test.txt')
df['Temperature'] = df['Temperature'].astype(float)
df = df[(df['Temperature'] >= 19) & (df['Temperature'] <= 41)]
quantile = df.quantile(0.75)
mean = df[df >= quantile].mean()
print(mean)

Basically what the code does, is that it imports csv file (test.txt) to pandas DataFrame, makes the values as float, then finds the values in specified range (19 to 41) and calculates the mean of 1/4 of last values.

fred
  • 93
  • 8