Print a specific row and column element from a Dataframe

Viewed 304

I have imported 4 csv files using a loop through pandas. Now I want to check where in the 'Channel' columns, the response is exceeding a certain threshold and print the time from 'Time' column and value of voltage from 'Channel' columns. I want only the first instance the value in each of the four channels, exceed threshold. So I want to do the following operations on it.

  1. I want to cut the threshold in half if the maximum in any of the channel is smaller than the threshold.

  2. After finding 'where the values of each of the columns' is crossing threshold, how do I print the 'Time' and value where this occurs?

Below is the code and how dfs looks like. Any help or suggestion would be great ! Thanks.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from pathlib import Path 

global df
df = pd.DataFrame([])

def function_plot(*xyzv, Plot_ShareY=True):    #x, y, z, v is filename 

    # data paths
    dps = [Path(f) for f in xyzv]
    
    for dp in dps:
        print(dp)

    #dataframes - loads the csv file from the paths created above
    dfs = [pd.read_csv(dp, delimiter=";", skiprows=(1, 2), decimal=",", na_values=['no info', '.']) for dp in dps]
    times = [df['Time'] for df in dfs]
    y_labels = ['A', 'B', 'C', 'D']    
    ch = [('Channel A', 'r'), ('Channel B', 'g'), ('Channel C', 'b'), ('Channel D', 'y')] 
    ch_vals = [('Channel A'), ('Channel B'), ('Channel C'), ('Channel D')] 
   
    threshold_abs = 0.003
    
    for df in dfs:
        if threshold_abs > np.max(ch_vals):
            threshold_abs = threshold_abs / 2
            print(threshold_abs)
          
        location = [df[ch_vals]>threshold_abs]
        print(location)    
       
    
    fig, *axes = plt.subplots(len(dfs), 1, figsize=(12, 8))
    fig.suptitle('Drops')   

    for x, ax, df, y_label in zip(times, axes[0], dfs, y_labels):
        for channel, colour in ch:
            ax.plot(x, df[channel], colour)
        ax.set_ylabel(y_label)
        ax.set_xlabel('Time [ms]')
        
    plt.show()

I want the output for question 2 to be like following:

['Time': 0.2, 'Channel A': 0.445, 'Time': 0.1, 'Channel B': 0.4385, 'Time': 0.2, 'Channel C': 0.425, 'Time': 0.4, 'Channel D': 0.4145, ]

1 Answers

Here you perform operation to calculate Boolean value:

location = [df[ch_vals]>threshold_abs]

that is why it returns true/false.

If you want to filter DataFrame to show only rows that are true in location variable, you can apply it like this:

df.loc[location]

ch_vals in your code is just a list of strings, when you use it to query DataFrame "df[ch_vals]" - it returns list of columns that are listed in the array from the DataFrame.

Related