functional-programming

What is `pandas.DataFrame.apply` actually operating on?

What is `pandas.DataFrame.apply` actually operating on? Question: I have two questions, but first I will give the context. I am trying to use a pandas DataFrame with some existing code using a functional programming approach. I basically want to map a function to every row of a DataFrame, expanding the row using the double-asterisk keyword …

Total answers: 1

How to write this python snippet in functional programming style?

How to write this python snippet in functional programming style? Question: How can I write the line below in functional python using e.g. toolz? dct1 = {1: 1, 2: 2} dct2 = {2:2} dct3 = {2:2, 3:3} common_keys = set(dct1.keys()) & set(dct2.keys()) & set(dct3.keys()) Asked By: gebbissimo || Source Answers: Actualy your question is very …

Total answers: 3

Convert a list comprehension to functional programming

Convert a list comprehension to functional programming Question: I have a list of dictionaries lst = [{‘a’: (1, 2, 3), ‘b’: (2, 3)}, {‘c’: (3, 6), ‘d’: (4, 8), ‘e’: (5, 10)}, {‘d’: (6, 12), ‘e’: (7, 14)}] For each key in each dictionary, I want to keep only the first element of the values. …

Total answers: 1

When and why to map a lambda function to a list

When and why to map a lambda function to a list Question: I am working through a preparatory course for a Data Science bootcamp and it goes over the lambda keyword and map and filter functions fairly early on in the course. It gives you syntax and how to use it, but I am looking …

Total answers: 1

What exactly is meant by "partial function" in functional programming?

What exactly is meant by "partial function" in functional programming? Question: According to my understanding, partial functions are functions that we get by passing fewer parameters to a function than expected. For example, if this were directly valid in Python: >>> def add(x,y): … return x+y … >>> new_function = add(1) >>> new_function(2) 3 In …

Total answers: 3

In python, what is a functional, and memory efficient way to read standard in, line by line?

In python, what is a functional, and memory efficient way to read standard in, line by line? Question: I have this https://stackoverflow.com/a/1450396/1810962 answer from another post which almost achieves it: import sys data = sys.stdin.readlines() preProcessed = map(lambda line: line.rstrip(), data) I can now operate on the lines in data in a functional way by …

Total answers: 1

python – Length of a list with the reduce() function

python – Length of a list with the reduce() function Question: I need some help to count the numbers of elements in a list by using the reduce function. def lenReduce(L): return reduce(lambda x: x + 1, L) With this one I get the following error message: TypeError: <lambda>() takes 1 positional argument but 2 …

Total answers: 3

Apply a list of Python functions in order elegantly

Apply a list of Python functions in order elegantly Question: I have an input value val and a list of functions to be applied in the order: funcs = [f1, f2, f3, …, fn] How to apply elegantly and not writing fn( … (f3(f2(f1(val))) … ) and also not using for loop: tmp = val …

Total answers: 2

Python fluent filter, map, etc

Python fluent filter, map, etc Question: I love python. However, one thing that bugs me a bit is that I don’t know how to format functional activities in a fluid manner like a can in javascript. example (randomly created on the spot): Can you help me convert this to python in a fluent looking manner? …

Total answers: 7