mapping

Is there a way to utilize polars mapping to make this code more efficient?

Is there a way to utilize polars mapping to make this code more efficient? Question: I have some polars code that functionally can do what I want, but I feel it is an inefficient implementation at best. I feel that there must be some way to achieve the same result with .map(), but I can’t …

Total answers: 1

Map the indices of 2d array into 1d array in Python

Map the indices of 2d array into 1d array in Python Question: Say I have a square 2d array (mat) and a 1d array (arr) which is the same length as the flattened 2d array (the values inside are different). Given the row and column index of the 2d array, how can I map it …

Total answers: 1

Python adding string elements from a string to a list

How to return each word and its respective length from a list Question: I am trying to find the length of all the words in a string and return a list with each word and its lengths next to it. Here is an example: A string of "hello python" would return ["hello 5", "python 6"] …

Total answers: 2

Python – Link 2 columns

Python – Link 2 columns Question: I want to create a data frame to link 2 columns together (customer ID to each order ID the customer placed). The row index + 1 correlates to the customer ID. Is there a way to do this through mapping? Data: invoice_df Order Id,Date,Meal Id,Company Id,Date of Meal,Participants,Meal Price,Type …

Total answers: 1

Python + ElasticSearch: Mapper Parsing Exceptions for join field

Python + ElasticSearch: Mapper Parsing Exceptions for join field Question: I’m using ElasticSearch 8.3.2 to store some data I have. The data consists of metabolites and several "studies" for each metabolite, with each study in turn containing concentration values. I am also using the Python ElasticSearch client to communicate with the backend, which works fine. …

Total answers: 1

Creating a json array in python from bunch of strings

Creating a json array in python from bunch of strings Question: In this script I’m parsing an html page using bs4 with urlopen(‘https://ahmia.fi/search/?q=’ + searchString) as webpage: rawResaults = webpage.read().decode() # Parsing the webpage to html using BeautifulSoup parsed_html = BeautifulSoup(rawResaults, features="lxml") then looping through each <li> element to get the value of element that …

Total answers: 1

PySpark: create column based on value and dictionary in columns

PySpark: create column based on value and dictionary in columns Question: I have a PySpark dataframe with values and dictionaries that provide a textual mapping for the values. Not every row has the same dictionary and the values can vary too. | value | dict | | ——– | ———————————————- | | 1 | {"1": …

Total answers: 2

Mapping set operations in python

Mapping set operations in python Question: I have a very simple problem, but today, I confess I have trouble finding the solution: I want to use the ‘&’ operator on python sets, but I don’t know how many sets I will have to deal with. Is it possible to use a mapping with python to …

Total answers: 1

How to map/join some index column of a multiindex pandas DataFrame?

How to map/join some index column of a multiindex pandas DataFrame? Question: Lets say I have a DataFrame df with a multi index [‘siec’, ‘geo’] (shown in italic): siec geo value a DE 1 a FR 2 and a mapping DataFrame mapping_df from geo to id_region with a single index [‘geo’]: geo id_region DE 10 …

Total answers: 1

Weird map behavior when using str.replace

Weird map behavior when using str.replace Question: I tried to use map with str.replace and I couldn’t get it to work properly. Can someone explain why? Here’s my code with a few examples: 1. code: print(list(map(str.replace, "abc", "ab", "b"))) output: [‘b’] 2. code: print(list(map(str.replace, "abc", "b", "c"))) output: [‘a’] 3. code: print(list(map(str.replace, "abc", "abc", "abc"))) …

Total answers: 2