<div dir="ltr"><div class="gmail_default" style="font-size:small">Hi Marc,</div><div class="gmail_default" style="font-size:small"><br></div><div class="gmail_default" style="font-size:small">This email list is for developers and contributors to the pandas package itself, rather than pandas users. I would recommend posting your question to Stack Overflow. If you don't get any satisfactory answers there, then you're welcome to open up a Usage Question on the pandas issue tracker at <a href="https://github.com/pandas-dev/pandas/issues">https://github.com/pandas-dev/pandas/issues</a>.</div><div class="gmail_default" style="font-size:small"><br></div><div class="gmail_default" style="font-size:small">Best,</div><div class="gmail_default" style="font-size:small">Richard</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sat, Jan 20, 2024 at 2:22 PM marc nicole <<a href="mailto:mk1853387@gmail.com">mk1853387@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Hello,<div><br></div><div>I have an initial dataframe with a random list of target cells (each cell being identified with a couple (x,y)).</div><div>I want to yield four different dataframes each containing the value of one of the contour (surrounding) cells of each specified target cell. </div><div><br></div><div>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.</div><div><br></div><div>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):</div><div>for now I have written this example which I think is not Pandas specific:</div><div><br></div><div><br></div><div><br></div><div><i>def select_target_values(dataframe, number_of_target_values):<br>    target_cells = []<br>    for _ in range(number_of_target_values):<br>        row_x = random.randint(0, len(dataframe.columns) - 1)<br>        col_y = random.randint(0, len(dataframe) - 1)<br>        target_cells.append((row_x, col_y))<br>    return target_cells<br><br><br>def select_contours(target_cells):<br>    contour_coordinates = [(0, 1), (1, 0), (0, -1), (-1, 0)]<br>    contour_cells = []<br>    for target_cell in target_cells:<br>        # random contour count for each cell<br>        contour_cells_count = random.randint(1, 4)<br>        try:<br>            contour_cells.append(<br>                [tuple(map(lambda i, j: i + j, (target_cell[0], target_cell[1]), contour_coordinates[iteration_]))<br>                 for iteration_ in range(contour_cells_count)])<br>        except IndexError:<br>            continue<br>    return contour_cells<br>       <br><br>def apply_contours(target_cells, contour_cells):<br>    target_cells_with_contour = []<br>    # create one single list of cells<br>    for idx, target_cell in enumerate(target_cells):<br>        target_cell_with_contour = [target_cell]<br>        target_cell_with_contour.extend(contour_cells[idx])<br>        target_cells_with_contour.append(target_cell_with_contour)<br>    return target_cells_with_contour<br>      <br>def create_possible_datasets(dataframe, target_cells_with_contour):<br>    all_datasets_final = []<br>    dataframe_original = dataframe.copy()     <br>      #check for nans<br>    list_tuples_idx_cells_all_datasets = list(filter(lambda x: utils_tuple_list_not_contain_nan(x),<br>                                                        [list(tuples) for tuples in list(itertools.product(<br>                                                            *target_cells_with_contour))]))<br>    target_original_cells_coordinates = list(map(lambda x: x[0],<br>                                                    [target_and_contour_cell for target_and_contour_cell in<br>                                                     target_cells_with_contour]))<br>    for dataset_index_values in list_tuples_idx_cells_all_datasets:<br>        all_datasets = []<br>        for idx_cell in range(len(dataset_index_values)):<br>            dataframe_cpy = dataframe.copy()<br>            dataframe_cpy.iat[<br>                target_original_cells_coordinates[idx_cell][1], target_original_cells_coordinates[idx_cell][<br>                    0]] = dataframe_original.iloc[dataset_index_values[idx_cell][1], dataset_index_values[idx_cell][0]]<br>            all_datasets.append(dataframe_cpy)<br>        all_datasets_final.append(all_datasets)<br>    return all_datasets_final</i><br></div><div><br></div><div><br></div><div>If you have a better Pandas approach (unifying all these methods into one that make use of dataframe methods only) please let me know.</div><div><br></div><div>thanks!</div><div><br></div><div><br></div></div>
_______________________________________________<br>
Pandas-dev mailing list<br>
<a href="mailto:Pandas-dev@python.org" target="_blank">Pandas-dev@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/pandas-dev" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/pandas-dev</a><br>
</blockquote></div>