Define the input Table as my Dataframe in python node

In this code I readed the excel file, but didn’t know how to use the input table in python.. How this can be done? and how can I work with indexing? because it gave me error that the rowID as index is string type and not integer.

image

import knime.scripting.io as knio
import pandas as pd

This example script simply outputs the node’s input table.

data = knio.input_tables[0]
input_file_path = r"C:\Users\uig44188\Desktop\Analysis\InputData\MyData.xlsx"

data = pd.read_excel(input_file_path)

Convert ‘TimeStamp’ to datetime for correct diff calculation

data[‘TimeStamp’] = pd.to_datetime(data[‘TimeStamp’], format=‘%d/%m/%Y %H:%M:%S:%f’)

Sort the data by ‘TimeStamp’

data.sort_values(‘TimeStamp’, inplace=True)

Identify indices where ‘Weight’ drops to zero or below (potential cycle starts)

zero_crossings = data.index[data[‘Weight’] <= 0].tolist()

Initialize a list to store the maximum of each cycle

maxima_each_cycle =

for i in range(len(zero_crossings)-1):
# Define the cycle as the data between this zero crossing and the next
cycle_data = data.iloc[zero_crossings[i]:zero_crossings[i+1]]

# Find the index and value of the maximum weight in this cycle
max_idx = cycle_data['Weight'].idxmax()
max_weight = cycle_data.loc[max_idx, 'Weight']

# Only consider the maximum if it is after the zero crossing and greater than 130
if max_idx > zero_crossings[i] and max_weight > data['Weight'].max() * 0.8 :
    maxima_each_cycle.append((cycle_data.loc[max_idx, 'TimeStamp'], max_weight))

Convert the list to a DataFrame

maxima_each_cycle_df = pd.DataFrame(maxima_each_cycle, columns=[‘TimeStamp’, ‘Max Weight’])

merged_data = pd.merge(data, maxima_each_cycle_df, on=‘TimeStamp’, how=‘left’)

knio.output_tables[0] = knio.Table.from_pandas(merged_data)

@hussein99 you might have to reset the index before exporting back to KNIME

Also you might want to consider to use another formatting when posting code to the forum, it makes the whole thing more readable:

4 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.