Python Return String From Function

Do you need to create a function that returns a string but you don’t know how? No worries, in sixty seconds, you’ll know! Go! πŸš€ A Python function can return any object such as a string. To return a string, create the string object within the function body, assign it to a variable my_string, and … Read more

Python – List of Lists to List of Strings. How?

Coding Challenge πŸ’¬ Coding Challenge: Given a nested Python list, i.e., a list of lists. How to convert it to a string list, so that each inner list becomes a single string? Consider the following examples: 1. Input: [[‘h’, ‘e’, ‘l’, ‘l’, ‘o’], [‘w’, ‘o’, ‘r’, ‘l’, ‘d’]] Output: [‘hello’, ‘world’] 2. Input: [[‘h’, ‘i’], … Read more

How to Apply a Function to Each Cell in a Pandas DataFrame?

Problem Formulation Given the following DataFrame df: πŸ’¬ Challenge: How to apply a function f to each cell in the DataFrame? For example, you may want to apply a function that replaces all odd values with the value ‘odd’. Solution: DataFrame applymap() The Pandas DataFrame df.applymap() method returns a new DataFrame where the function f … Read more

pd.agg() – Aggregating Data in Pandas

The name agg is short for aggregate. To aggregate is to summarize many observations into a single value that represents a certain aspect of the observed data. The .agg() function can process a dataframe, a series, or a grouped dataframe. It can execute many aggregation functions, e.g. β€˜mean’, β€˜max’,… in a single call along one … Read more