Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Those who can, do; those who can't, simulate.


devel / comp.lang.python / How to replace a cell value with each of its contour cells and yield the corresponding datasets seperately in a list according to a Pandas-way?

SubjectAuthor
o How to replace a cell value with each of its contour cells and yield the correspmarc nicole

1
How to replace a cell value with each of its contour cells and yield the corresponding datasets seperately in a list according to a Pandas-way?

<mailman.94.1705840513.15798.python-list@python.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=24913&group=comp.lang.python#24913

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!rocksolid2!news.neodome.net!fu-berlin.de!uni-berlin.de!not-for-mail
From: mk1853...@gmail.com (marc nicole)
Newsgroups: comp.lang.python
Subject: How to replace a cell value with each of its contour cells and yield
the corresponding datasets seperately in a list according to a
Pandas-way?
Date: Sun, 21 Jan 2024 13:37:43 +0100
Lines: 113
Message-ID: <mailman.94.1705840513.15798.python-list@python.org>
References: <CAGJtH9RvLNeHFQSVk4b-TuYKKLgVbvAnoiQvC37vw5HGm2YjHg@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de hNvH7mjvrFPuCwmnkXBDvQ7QsOZl3+fIfiYGorOhOuuA==
Cancel-Lock: sha1:bPbLJHRYDKlbIjtCR+YmLWSxVXE= sha256:VkS6MYyaWFeqCckWr6x1YrQfIV26QgTPfSTxHptFnaA=
Return-Path: <mk1853387@gmail.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=gmail.com header.i=@gmail.com header.b=fxVtJfZ6;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.006
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'def': 0.04; 'containing':
0.05; 'random': 0.05; 'skip:\xc2 30': 0.07; 'subject:value': 0.07;
'cell.': 0.09; 'dataframe': 0.09; 'pandas': 0.09; 'skip:* 30':
0.09; 'skip:[ 20': 0.09; 'subject:list': 0.11; '(1,': 0.16; '1),':
0.16; 'cell.\xc2\xa0': 0.16; 'dataframes': 0.16; 'separately':
0.16; 'subject:cell': 0.16; 'subject:each': 0.16; 'subject:way':
0.16; 'tuples': 0.16; 'yield': 0.16; '\xc2\xa0for': 0.16; 'to:addr
:python-list': 0.20; 'written': 0.22; 'subject:How': 0.23;
'thanks!': 0.24; 'approach': 0.31; 'think': 0.32; 'specified':
0.32; 'message-id:@mail.gmail.com': 0.32; 'received:google.com':
0.34; 'from:addr:gmail.com': 0.35; 'cell': 0.36; 'count': 0.36;
'target': 0.36; 'couple': 0.37; 'using': 0.37; 'changes': 0.39;
'single': 0.39; 'list': 0.39; 'use': 0.39; 'methods': 0.39;
'define': 0.40; 'hello,': 0.40; 'want': 0.40; 'four': 0.60;
'initial': 0.61; 'identified': 0.62; 'skip:r 20': 0.64; 'skip:t
40': 0.64; 'let': 0.66; 'skip:t 20': 0.66; '8bit%:7': 0.67;
'skip:t 30': 0.67; 'skip:e 20': 0.67; 'choose': 0.67; 'know.':
0.68; 'surrounding': 0.69; 'skip:t 60': 0.70; 'only)': 0.84;
'subject: \n ': 0.84; 'skip:d 30': 0.86
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20230601; t=1705840510; x=1706445310; darn=python.org;
h=to:subject:message-id:date:from:mime-version:from:to:cc:subject
:date:message-id:reply-to;
bh=4Y4DfovFdrKK4K5C73ePlB0pYM8J8xqYQ0jpyKS2THQ=;
b=fxVtJfZ67l7ztesv4Zum4yO0GXZjQMFRBP1qmictWoa6f3aFvkV/KCORyAv48GbAhF
mhX9hostqB3TBfNaRvohfhpktM/GC+PYDKGd1OFPNQyiw0wZSrH4mO9gjwer4GZAhGBX
/k6AC5Afy5yoblSosefZMNmGi0udwpcT9iuBSunj/Xyo0yktCpDSuBB55lnfF8cTLlkC
SR6Gmxs2/7EXv4NWif+kWMXPXX+D6i20DsHVmL0W/BS6tpZ3ecrNEcev7Pvonz9tLP5t
oLZOxwJjYyxx59l93XRdwEtHG9aKLw3wG5GVOCIysu1XRH9rm8TtGlu+XTcfpKvWrK0v
zPAQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20230601; t=1705840510; x=1706445310;
h=to:subject:message-id:date:from:mime-version:x-gm-message-state
:from:to:cc:subject:date:message-id:reply-to;
bh=4Y4DfovFdrKK4K5C73ePlB0pYM8J8xqYQ0jpyKS2THQ=;
b=qA7/RA085JZIylDEQ0KpO6mnz5IcpI0O4gT+0DbV9wytNStdqbo9jkLLiE5BI+m7nk
5yP7eiRrHnZGFrnH8qxcaRC2LWYEtyumTT6m9FNk/5BlcyV3Poc3HB/x4XvpAr0s9BEr
IyMipwfPzOkHRe3H0O64yChb2JfyGH1A+NcIwxK6NlvaoZKvp1n/NPLHYYS8ZDxmLPrK
4CIuDCB1krsgNcOkl2lXjzLd1v4vtb5e5C3quyBXG+HebcQIUb/7JgrjZqIcRFvhT24N
+TiVji7RjUy+2we+fs1tiHk6osxo0zCHpqDIKFx+dfj75lFeyh7Dv2UY8LNLK/8GdvL8
WLvA==
X-Gm-Message-State: AOJu0YxWDGeUI30QV31YWuPOze/OofOftDpJ8RsCbgpnnOwDAV9n1X5f
cl/My7jnjRWacucA9U4UPRbQ/7kY5nI9fdQClfPDf1tDZFMnWlNgQwhSlVmakDmV67KxkZwg/1c
hAS1VLvM0rW4QneKRap5t2ZcM6sBXz0ng7SM=
X-Google-Smtp-Source: AGHT+IEUtRxxja42oSlNszX2jZ16HwXneaCcEsvu7wrIeutfnath6f+hJWe82l8v3B29ItrRDK0AjVqV78uLeBI/gP4=
X-Received: by 2002:a05:6871:2b23:b0:210:dda5:1b12 with SMTP id
dr35-20020a0568712b2300b00210dda51b12mr2513749oac.110.1705840509964; Sun, 21
Jan 2024 04:35:09 -0800 (PST)
X-Content-Filtered-By: Mailman/MimeDel 2.1.39
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <CAGJtH9RvLNeHFQSVk4b-TuYKKLgVbvAnoiQvC37vw5HGm2YjHg@mail.gmail.com>
 by: marc nicole - Sun, 21 Jan 2024 12:37 UTC

Hello,

I have an initial dataframe with a random list of target cells (each cell
being identified with a couple (x,y)).
I want to yield four different dataframes each containing the value of one
of the contour (surrounding) cells of each specified target cell.

the surrounding cells to consider for a specific target cell are : (x-1,y),
(x,y-1),(x+1,y);(x,y+1), specifically I randomly choose 1 to 4 cells from
these and consider for replacement to the target cell.

I want to do that through a pandas-specific approach without having to
define the contour cells separately and then apply the changes on the
dataframe (but rather using an all in one approach):
for now I have written this example which I think is not Pandas specific:

*def select_target_values(dataframe, number_of_target_values):
target_cells = [] for _ in range(number_of_target_values): row_x
= random.randint(0, len(dataframe.columns) - 1) col_y =
random.randint(0, len(dataframe) - 1) target_cells.append((row_x,
col_y)) return target_cellsdef select_contours(target_cells):
contour_coordinates = [(0, 1), (1, 0), (0, -1), (-1, 0)] contour_cells =
[] for target_cell in target_cells: # random contour count for
each cell contour_cells_count = random.randint(1, 4) try:
contour_cells.append( [tuple(map(lambda i, j: i + j,
(target_cell[0], target_cell[1]), contour_coordinates[iteration_]))
for iteration_ in range(contour_cells_count)]) except
IndexError: continue return contour_cellsdef
apply_contours(target_cells, contour_cells): target_cells_with_contour =
[] # create one single list of cells for idx, target_cell in
enumerate(target_cells): target_cell_with_contour = [target_cell]
target_cell_with_contour.extend(contour_cells[idx])
target_cells_with_contour.append(target_cell_with_contour)return
target_cells_with_contourdef create_possible_datasets(dataframe,
target_cells_with_contour): all_datasets_final = []
dataframe_original = dataframe.copy() #check for nans
list_tuples_idx_cells_all_datasets = list(filter(lambda x:
utils_tuple_list_not_contain_nan(x),
[list(tuples) for tuples in list(itertools.product(

*target_cells_with_contour))])) target_original_cells_coordinates =
list(map(lambda x: x[0],
[target_and_contour_cell for target_and_contour_cell in
target_cells_with_contour])) for
dataset_index_values in list_tuples_idx_cells_all_datasets:
all_datasets = [] for idx_cell in range(len(dataset_index_values)):
dataframe_cpy = dataframe.copy() dataframe_cpy.iat[
target_original_cells_coordinates[idx_cell][1],
target_original_cells_coordinates[idx_cell][ 0]] =
dataframe_original.iloc[dataset_index_values[idx_cell][1],
dataset_index_values[idx_cell][0]]
all_datasets.append(dataframe_cpy)
all_datasets_final.append(all_datasets) return all_datasets_final*

If you have a better Pandas approach (unifying all these methods into one
that make use of dataframe methods only) please let me know.

thanks!

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor