0

I currently have a function where I am reading in over 25 .csv files. At the moment, I am taking the average over a fixed interval for the same starting and ending sample for each file (see in my code, below).

My goal is now to adjust the starting time to when Temp>50.
My pseudocode is like this:

Current: avg_Temp = df.iloc[63-1:115+1]["Temp"].mean()

Objective: avg_Temp = df.iloc[df[Temp>50]:115+1]["Temp"].mean()

What is the best way to accomplish this?

def plot_data(filename, fig_ax, color):
    
    df = pd.read_csv(f, sep=',',skiprows=24)    # Read in the csv.
    df.columns=['sample','Time','ms','Temp']    # Set the column names
    df=df.astype(str)                           # Set the data type as a string.

    df["Temp"] = df["Temp"].str.replace('\+ ', '').str.replace(' ', '').astype(float) # Convert to float
   
    # Take the average of the data from the Temp column, starting from sample 63 until sample 115.
    avg_Temp = df.iloc[63-1:115+1]["Temp"].mean()
    

enter image description here

2
  • Do you mean df.loc[df['temp']>50, 'Temp'].head(50).mean(0)? Commented Dec 13, 2020 at 19:30
  • Basically, I am trying to start the average when the Temp value is greater than 50; rather than setting a fixed starting value for each one. Commented Dec 13, 2020 at 19:38

1 Answer 1

1

Probably something like this

idx = (df['Temp'] > 50).idxmax()
avg_Temp = df.iloc[idx:115+1]["Temp"].mean()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.