nested-loops

Duplicate pairs of rows side by side in pandas given certain condition

Duplicate pairs of rows side by side in pandas given certain condition Question: I have the following code: import pandas as pd data = { ‘Col1’: [‘John 1’, ‘John 2’, ‘John 3’, ‘Kyle 1’, ‘Kyle 3’, ‘Kyle 2’], ‘Col2’: [‘B’, ‘C’, ‘E’, ‘F’, ‘F’, ‘S’], ‘Col3’: [‘1’, ‘1’, ‘1’, ‘1’, ‘1’, ‘2’] } df = …

Total answers: 4

Simplifying nested loops using recursion

Simplifying nested loops using recursion Question: Let’s assume we have a three-dimensional array x. For each k, I want to have the mean of x[:, :, k] stored as out[k]. The task is simple if it is a 3D or 4D matrix, with the for loop like this: x = np.random.normal(0, 1, [4, 4, 3]) …

Total answers: 2

Nested for loop to create list of urls

Nested for loop to create list of urls Question: I am looking for a way to create a list of urls by modifying the name of the country and the date/time. For each country, each day and each hour of the day I need to create a unique url. import datetime start_date = datetime.date(2019, 4, …

Total answers: 1

For loop with conditional statements not working as expected (Pandas)

For loop with conditional statements not working as expected (Pandas) Question: #Loop through example dataset for id in df[‘val_id’]: #total assigned value df[‘tot_val’] = 0 #loop through facilities for fac in df[‘fac_id’]: if df[‘product’].isin([‘XL’, ‘CL’, ‘DL’]).all(): df[‘row’] = min(df[‘our_val_amt’] – df[‘tot_val’], df[‘val_against’]) else: df[‘row’] = 0 df[‘tot_val’] = df[‘tot_val’] + df[‘row’] df[‘row’] = df[‘row’] + …

Total answers: 1

How to add values to a Pandas Dataframe using a nested for loop?

How to add values to a Pandas Dataframe using a nested for loop? Question: I have two data frames containing float values. The first one is a 1 column data frame that contains positions. The second one is a matrix of ncol equal to the number of IDs and nrows equal to the nrow of …

Total answers: 2