跳到主要内容

🥷 [S2] Challenge 04

· 阅读需 1 分钟

Just KNIME It, Season 2 / Challenge 04 reference

Process

Divide area A and area B first, then processed according to different time requirements through the Python node.

spread equally across 12 months

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

df = knio.input_tables[0].to_pandas()

# Calculate the monthly expense allocation
df['exp'] /= 12

# Duplicate the rows for each month
df = df.loc[df.index.repeat(12)].reset_index(drop=True)
df['time_range'] = pd.date_range(start='2022-01-01', periods=len(df), freq='MS').strftime('%b')


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

spread equally across 4 quarters

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

df = knio.input_tables[0].to_pandas()

# Define the number of quarters
num_quarters = 4

# Expand the table with a row for each quarter
df_expanded = pd.concat([df] * num_quarters, ignore_index=True)

# Add a column for the quarter index
df_expanded['time_range'] = ['Q{}'.format(i+1) for i in range(num_quarters)] * len(df)

# Calculate the amount of exp for each quarter for each project
exp_per_quarter = df_expanded['exp'] / num_quarters

# Add a column for the exp for each quarter
df_expanded['exp'] = exp_per_quarter

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

Another thoughts

BTW, this is just for practice. I do believe it is more easy to create a constant time_range table, then just use cross join node.