To create an empty list in Python, you can use two ways. First, the empty square bracket notation [] creates a new list object without any element in it. Second, the list() initializer method without an argument creates an empty list object too.
Both approaches are shown in the following code:
# Way 1 to create an empty list:
my_list = [] # Way 2 to create an empty list:
my_list = list()
Next, you’ll learn many more related Python concepts you need to know that concern creation of lists. Keep reading to keep improving your skills and answer any subsequent question you may have!
Python list() — Quick Guide
Pythonโs built-in list() function creates and returns a new list object. When used without an argument, it returns an empty list. When used with the optional iterable argument, it initializes the new list with the elements in the iterable.
You can create an empty list by skipping the argument:
>>> list()
[]
If you pass an iterableโsuch as another list, a tuple, a set, or a dictionaryโyou obtain a new list object with list elements obtained from the iterable:
You can create an empty list of lists do not use [[]] * n because this creates a nested list that contains the same empty list object n times which can cause problems because if you update one, all inner lists change!
To create an empty list of lists with n empty inner lists, use the list comprehension statement [[] for _ in range(n)] that creates a fresh empty list object n times.
n = 5
my_list = [[] for _ in range(n)]
print(my_list)
# [[], [], [], [], []]
List comprehension is a powerful Python feature and I’ve written a full blog tutorial on it—feel free to watch my general explainer video and read the associated blog article!
Follow these three easy steps to create an empty list and append values to it in a for loop:
my_list = [] creates the empty list and assigns it to the name my_list.
for i in range(10): initializes the for loop to be repeated 10 times using loop variable i that takes on all values between 0, 1, …, 9.
my_list.append(i) is the loop body that appends the integer value of the loop variable i to the list.
Here’s the code example:
my_list = []
for i in range(10): my_list.append(i) print(my_list)
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
However, a better Python one-liner alternative is using list comprehension for this:
my_list = [i for i in range(10)]
Python Create List of Empty Strings
To create a list of n empty strings, you can use the expression [''] * n because it places the same empty string literal '' into the list n times. This doesn’t cause any problems due to the fact that all list elements refer to the same empty string object because strings are immutable and cannot be modified anyways.
To create a list of n dictionaries, each dict being empty, use the list comprehension statement [dict() for _ in range(n)] with the underscore _ as a throw-away “loop variable” and the dict() built-in dictionary creation function.
my_list = [dict() for _ in range(10)]
print(my_list)
# [{}, {}, {}, {}, {}, {}, {}, {}, {}, {}]
Note that if you update one of the dictionaries, all other dictionaries are unaffected by this because we really created n independent dictionary objects.
To create an empty list of class objects, you can use list comprehension statement my_list = [MyClass() for _ in range(n)] that repeats n times the creation of an empty class object MyClass and adding it to the list. You can then later change the contents of the n different MyClass objects.
class MyClass(object): pass my_list = [MyClass() for _ in range(5)] print(my_list)
# [<__main__.MyClass object at 0x000001EA45779F40>, <__main__.MyClass object at 0x000001EA47533D00>, <__main__.MyClass object at 0x000001EA475334C0>, <__main__.MyClass object at 0x000001EA4758E070>, <__main__.MyClass object at 0x000001EA4758E4F0>]
Python Create Empty List of Type
Python is a dynamic language so there is no concept of a “list of type X”. Instead of creating a list of a fixed type, simply create an empty list using [] or list() and assign it to a variable such as my_list. Using the variable, you can then fill into the existing list any data type you want!
Here we create an empty list and fill in an integer, a list, and a string—all into the same list!
my_list = []
# Alternative: my_list = list() # Add integer to list:
my_list.append(42) # Add list to list:
my_list.append([1, 2, 3]) # Add string to list:
my_list.append('hello world') # Print all contents of list:
print(my_list)
# [42, [1, 2, 3], 'hello world']
Python Create Empty List of Integers
To initialize a list with certain integers such as zeroes 0, you can either use the concise list multiplication operation [0] * n or you use list comprehension [0 for _ in range(n)].
To create an empty list and later add one tuple at-a-time to it, first initialize the empty list using the [] square bracket operator and then use the list.append(t) to append one tuple t at a time.
Here we add three tuples to the initially empty list:
Input: You have some data in a CSV file stored in 'my_file.csv' where the first row is the header and the remaining rows are values associated to the column names in the header.
Desired Output: You want to store the data in an XML file 'my_file.xml' so that each row is represented by an XML <row> tag and each column value is associated with a specific column header tag.
You can convert a CSV to an XML using the following approach:
Read the whole CSV file into your Python script.
Store the first row as header data that is needed to name your custom XML tags (e.g., <Name>, <Job>, <Age>, and <Income> in our example).
Create a function convert_row() that converts each row separately to an XML representation of that row using basic string formatting.
Iterate over the data row-wise using csv.reader() and convert each CSV row to XML using your function convert_row().
Here’s the code for copy&paste:
# Convert CSV file to XML string
import csv filename = 'my_file.csv' def convert_row(headers, row): s = f'<row id="{row[0]}">\n' for header, item in zip(headers, row): s += f' <{header}>' + f'{item}' + f'</{header}>\n' return s + '</row>' with open(filename, 'r') as f: r = csv.reader(f) headers = next(r) xml = '<data>\n' for row in r: xml += convert_row(headers, row) + '\n' xml += '</data>' print(xml)
You can also use pandas instead of the csv module to read the CSV file into your Python script. Everything else remains similar—I highlighted the lines that have changed in the following code snippet:
import pandas as pd def convert_row(headers, row): s = f'<row id="{row[0]}">\n' for header, item in zip(headers, row): s += f' <{header}>' + f'{item}' + f'</{header}>\n' return s + '</row>' df = pd.read_csv("my_file.csv")
headers = df.columns.tolist()
xml = '<data>\n' for _, row in df.iterrows(): xml += convert_row(headers, row) + '\n' xml += '</data>'
print(xml)
You can customize this behavior of separating two lines using a single newline character '\n' by changing the default end='\n' argument of the print() function to your desired string.
Another way to skip a line in the Python output is to add an empty print() statement that will just print an empty line and do nothing else.
Python’s newline character to indicate the end of a line of text is \n.
If you print a string to the shell using the built-in print() function, Python automatically adds a newline character\n at the end.
PYTHON CODE:
print('hello\nworld\n\nPython is great!') OUTPUT:
hello
world Python is great!
For example, if you iterate over the text in a file using a forloop and print each line in the loop body, the lines are separated with single new lines.
#################################
# File: my_filename.txt #
#################################
# My #
# File #
# Content #
################################# with open('my_filename.txt', 'r') as my_file: for line in my_file.readlines(): print(line) # Output:
My
File
Content
You can customize this behavior of separating two lines using a single newline character '\n' by changing the default end='\n' argument of the print() function to your desired string.
For example, you can skip two lines in Python using print(my_string, end='\n\n') by chaining two newline characters '\n\n'.
with open('my_filename.txt', 'r') as my_file: for line in my_file.readlines(): print(line, end='\n\n') # Output:
My File Content # End Output
Another way to skip a line in the Python output is to add an empty print() statement that will just print an empty line and do nothing else.
with open('my_filename.txt', 'r') as my_file: for line in my_file.readlines(): print(line) print() # Output:
My File Content # End Output
Question: Does Python have a for each or foreach loop? If so, how does it work? If not, what is the alternative?
This article will shed light on these questions. I’ll give you the summary first and dive into the details later:
Python has three alternatives to the “for each” loop:
A simple for ... in ... loop
A map() function
A list comprehension statement.
You’ll learn about those alternatives in the following paragraphs, so keep reading!
Let’s get started with the most important question:
What is a “Foreach Loop”?
Definition: A foreach or for each loop is a programming control flow statement for iterating over elements in a sequence or collection. Unlike other loop constructs, the foreach loop iterates over all elements rather than maintaining a counter, loop variable, or checking a condition after each loop iteration.
Figure: Example of a for each loop (pseudocode) that iterates over elements 10, 20, and 30 and prints their value.
Here are three examples of a foreach loop in three different programming languages PHP, C#, and Perl:
// PHP
foreach ($set as $value) { // Do something to $value;
} // C#
foreach (String val in array) { console.writeline(val);
} // Perl
foreach (1, 2, 3, 4) { print $_;
}
Does Python have a foreach Loop?
The Python language doesn’t support the keywords foreach or for each loops in a literal syntactical way. However, “for each” in Python is done using the “for … in …” expression. For example, to iterate over each element in the list[10, 20, 30] in Python, you’d write for x in [10, 20, 30].
Here’s a full Python code example with semantical equivalence to a “foreach” statement:
# 'foreach' or 'for each' in Python is done using 'for'
for x in [10, 20, 30]: print(x)
“For Each” Meaning “Apply Function to Each Element”
If you’re reading this and you haven’t been satisfied with the answers provided so far, chances are that you’re really searching for the map function functionality in Python.
Many programming languages with “for each” support provide a syntax that applies a function to each element of an iterable like so:
# Other programming languages:foreach(function, iterable)
This can be done in Python by means of the map() function:
# Python:
map(function, iterable)
Here’s a simple example of how you’d use the map() function in Python that applies the function f to each element of the list [1, 2, 3], incrementing each of its elements by 1 to obtain [2, 3, 4]:
Python’s list comprehension feature is syntactical sugar to create a new iterable by applying a (possibly identity) function to each element of an existing iterable.
Many coders would view the list comprehension feature as Python’s way to provide a functional “foreach” statement because it enables you to perform a function “for each” element of an iterable such as a sequence.
List comprehension is a compact way of creating lists. The simple formula is [expression + context].
Expression: What to do with each list element?
Context: What elements to select? The context consists of an arbitrary number of for and if statements.
The example [x+10 for x in [1, 2, 3]] creates the list [11, 12, 13].
lst = [1, 2, 3]
new_lst = [x+10 for x in lst]
print(new_lst)
# [11, 12, 13]
You can watch my explainer video on list comprehension in case you’re interested in how it works:
Now that the overview is complete, let’s connect to the database, filter, and output the results.
Connect to a SQLite Database
This code connects to an SQLite database and is placed inside a try/except statement to catch any possible errors.
try: conn = sqlite3.connect('finxter_users.db') cur = conn.cursor() except Exception as e: print(f'An error occurred: {e}.') exit()
The code inside the try statement executes first and attempts to connect to finxter_users.db. A Connection Object (conn), similar to below, is produced, if successful.
<sqlite3.Connection object at 0x00000194FFBC2140>
Next, the Connection Object created above (conn) is used in conjunction with the cursor() to create a Cursor Object. A Cursor Object (cur), similar to below, is produced, if successful.
<sqlite3.Cursor object at 0x0000022750E5CCC0>
Note: The Cursor Object allows interaction with database specifics, such as executing queries.
If the above line(s) fail, the code falls inside except capturing the error (e) and outputs this to the terminal. Code execution halts.
Prepare the SQLite Query
Before executing any query, you must decide the expected results and how to achieve this.
try: conn = sqlite3.connect('finxter_users.db') cur = conn.cursor() fid_list = [30022192, 30022450, 30022475] fid_tuple = tuple(fid_list) f_query = f'SELECT * FROM users WHERE FID IN {format(fid_tuple)}' except Exception as e: print(f'An error occurred: {e}.') exit()
In this example, the three (3) highlighted lines create, configure and save the following variables:
fid_list: this contains a list of the selected Users’FIDs to retrieve.
fid_tuple: this converts fid_list into a tuple format. This is done to match the database format (see above).
f_query: this constructs an SQLite query that returns all matching records when executed.
Query String Output
If f_query was output to the terminal (print(f_query)), the following would display. Perfect! That’s exactly what we want.
SELECT * FROM users WHERE FID IN (30022192, 30022450, 30022475)
Executing the SQLite Query
Let’s execute the query created above and save the results.
try: conn = sqlite3.connect('finxter_users.db') cur = conn.cursor() fid_list = [30022192, 30022450, 30022475] fid_tuple = tuple(fid_list) f_query = f'SELECT * FROM users WHERE FID IN {format(fid_tuple)}' results = cur.execute(f_query)
except Exception as e: print(f'An error occurred: {e}.') exit()
The highlighted line appends the execute() method to the Cursor Object and passes the f_query string as an argument.
If the execution was successful, an iterableCursor Object is produced, similar to below.
<sqlite3.Cursor object at 0x00000224FF987A40>
Displaying the Query Results
The standard way to display the query results is by using a for a loop. We could add this loop inside/outside the try/except statement.
try: conn = sqlite3.connect('finxter_users.db') cur = conn.cursor() fid_list = [30022192, 30022450, 30022475] fid_tuple = tuple(fid_list) f_query = f'SELECT * FROM users WHERE FID IN {format(fid_tuple)}' results = cur.execute(f_query)
except Exception as e: print(f'An error occurred: {e}.') exit() for r in results: print(r)
conn.close()
The highlighted lines instantiate a for loop to navigate the query results one record at a time and output them to the terminal.
WooCommerce is the best among the top eCommerce software which is free. It is based on WordPress platform. Setting up a shopping cart is quite easy with WooCommerce.
In previous articles, we have seen some WooCommerce-based code for the following utilities.
The WooCommerce product showcase displays the information that meets the customer demand. Sometimes preliminary details cannot convey all and let the customer identify their destiny.
This is something that needs adding additional information using custom-field integration. WooCommerce plugins support adding custom fields to products via the admin interface.
We will see those options below and their usage methodologies of them.
Why a WooCommerce shop admin needs Custom Fields
Before adding custom fields to a product, learn why a WooCommerce shop needs this.
Because, some of the shops have a routine product gallery with usual data like title, image and price. In that case, the custom fields are not required.
The below list shows the advantages of this custom fields feature on a WooCommerce shop.
To add product metadata that is for displaying additional information about products.
To allow customers to provide more specifications on the products to purchase.
To add user-friendly interactive fields to save their effort on giving customizations.
To add promotions, offers, discounts and all.
To support end-user to specify a delivery date, time and place.
To apply tax, and shipping by toggling the custom switch.
Types of custom fields creation supported by WooCommerce plugins
The available WooCommerce plugins can add two types of custom fields for products.
Data fields โ The field accepts information to be displayed on the UI.
Add-on fields โ Fields that collect data from the user on the storefront.
1. Data fields โ The field accepts information to be displayed on the UI
This custom field is for showing more information apart from the regular title, price and all. This will help to show product metadata about features, version, rating and all.
For example, a monitor, to show extra information like,
Brand
Dell
Model
Dell 20H
Screen Size
23
Features
HDMI, Anti-glare
2. Add-on fields โ Fields that collect data from the user on the storefront
This type of custom field is about creating options for the customer to interact with. It is to allow adding more specifications or customization on the pursued product.
It is for adding personalized information by the customers on the product purchase. Example,
A multi-option field to select the size or color of a T-shirt.
A text field to collect a brand name to be engraved on a purchased pen.
Methods of adding custom fields display in WooCommerce
Using WooCommerce plugins.
Adding code to display custom fields for products.
Method 1: Using the plugin
We have taken three plugins to support adding custom fields to a product in a WooCommerce shop. Those are having advanced features and are also easy to set up.
Advanced Product Fields for WooCommerce
Product Addons for WooCommerce
Advanced Custom Fields
1. Advanced Product Fields for WooCommerce
This plugin is used for adding additional fields to the WooCommerce product page. It helps to embed product add-on fields to allow customers to personalize the order.
Look into the plugin details and prerequisites below.
Version
4.5 or higher
Active installations
20,000+
PHP version
5.6 or higher
Last updated
2 weeks ago
Features
The custom product field selected on the product page can be carried over to the cart and checkout pages. It is persistent on the entire flow until the order receipt.
Smooth product field builder in the backend with a seamless experience.
It has the option to add multiple prices to manipulate the base price of the products on selection.
It supports all possible interactive fields with the show hide option.
It allows products and custom-field mapping interfaces to control the visibility.
Download this plugin from the official WordPress repository. Then install and activate it for the WooCommerce shop. If you are new to WordPress, then see here how to set up a plugin.
Go to Product -> Add New via the WooCommerce admin menu.
Click โCustom fieldsโ under the โProduct dataโ panel.
Configure โField group layoutโ settings. It is to specify the position of the label or a field note if any. Also, it helps to mark a field as mandatory.
Select โAdd the first fieldโ or โAdd fieldโ and specify field type, label, and all.
Add conditional rules to add dependencies between fields to be displayed.
This is the WooCommerce admin interface to add the custom fields for products. I have added the Size option for the product as a ratio group.
This will show the added custom field on the product front end as a radio option.
2. Product Addons for WooCommerce
This plugin says the purpose clearly by its name itself. Yes, it is to add more add-on fields to let the customer interact with the shop to personalize the purchase.
An easy custom form builder allows for building a field group for the WooCommerce shop pages. Have a look at the version and other details below.
It saves the customerโs personalized data via a custom field added for products in the backend.
For builder helps to design a group of the custom fields.
Allows more types of fields like text, number and email fields, combo fields and all.
In the base version, it allows adding <p> and <h*> tags to display product meta in the front end.
Steps to use
There are a few steps to add and display a custom field on a product page of a WooCommerce theme.
Go to Products->Custom Product Addons via the WordPress admin menu.
Add a new form of product custom fields group and publish it.
Go to Products and open a new or edit product panel.
Choose โCustom Product Optionsโ from the Product data group.
Check the form to add a mapping between custom fields and products.
See the following two screenshots to add custom fields and map them for the product.
Then, the WooCommerce shop will show the custom fields on the single product page.
3. Advanced Custom Fields
It supports full control of the WordPress edit screen and custom form data.
WordPress Version
4.7 or higher
Active Installations
2+ million
PHP Version
5.6 or higher
Last updated
6 days ago
The simple and intuitive plugin has powerful functions and over 30 field types
Features
It provides powerful functions to customize and add custom fields for the product
Good and simple backend interface to add custom field group on a need basis.
Supports 30+ field types to set more data apart from the standard custom product fields.
Steps to customize
Download or install via admin to enable this plugin before going to read the below steps.
Add and customize the field group.
Add form fields into the group.
Add data for the custom fields on the product page.
Display the product information on the front end.
Step 1: Add and customize field group
Go to Custom Fields via the admin menu. Add a new field group and specify the UI location and other settings.
It helps to set states and styles to add and show the custom fields on a product page. It manages the relative position and alignment of the field on the UI.
Fields can also be mapped for posts, products, pages and more.
Step 2: Add form fields into the group
The field add interface allows the following data to enter. For combo fields, it asks to enter multiple options to the loaded.
label, name, type.
Field notes or helps text.
If the field is required or not.
Place holder and default value.
Content to prepend or append.
Character restriction, Rules.
Field wrapper classes or ids.
Step 3: Add data for the custom fields based on products
If the custom field group is mapped for the location of product pages, then the add/edit page will show it.
Add the data for the custom fields which will be displayed on the WooCommerce product at the shop.
##image
Step 4: Display the product information on the front-end
Then display the custom fields data on the product single page at the front-end.
In this plugin, we need to add a simple shortcode to display the custom fields on the product page.
The [acf field=โ<field-id or slug>โ] shortcode is used for displaying the custom field group.
There are a couple of functions in WordPress to display the fields for a WooCommerce product.
<php
the_field("<field-id or slug>");
?>
or
<php
$customFieldGroup = get_field("<field-id or slug>");
echo $customFieldGroup;
?>
Method 2: Adding custom fields via the program
Thus, we have seen how to add product custom fields in a WooCommerce platform using plugins. If you want to implement the same without using plugins, itโs very simple with a few steps.
These steps render the custom fields on the WooCommerce product page. After collecting the user inputs in the product page, the custom code will display them on the cart page.
Steps to implement this method by adding custom code are listed below.
Add a custom field in the product data panel on the WooCommerce admin.
Save added custom field data into cart metadata.
Display an input field in a product single page.
Passing the values on the cart and checkout pages.
Add this custom code in the WordPress active themeโs function.php file.
1. Adding custom fields in the product data panel
Create code for displaying custom fields in the WooCommerce product data panel. The woocommerce_product_options_general_product_data hook is used to call this code.
/* Add product meta field for the WooCommerce admin */
function woocommerce_custom_select_dropdown(){ $select = woocommerce_wp_select(array( 'id' => '_select_color', 'label' =>__('Select color', 'woocommerce'), 'options' => array( 'Black' => __('Black','woocommerce'), 'Blue' => __('Blue','wooocommerce'), 'Pink'=> __('Pink','woocommerce') ), ));
}
add_action('woocommerce_product_options_general_product_data', 'woocommerce_custom_select_dropdown');
This example adds a dropdown as a custom field for products. So, it uses the woocommerce_wp_select function to set the following parameters.
id
label
description
desc_tip
options
There are similar functions to add other types of form fields. For example,
woocommerce_wp_textarea_input
woocommerce_wp_checkbox
woocommerce_wp_hidden_input
2. Save the user input in the custom fields to cart metadata.
Then, another action hook is added to save the custom field value on the product meta. This will be called on saving the product details. It calls the woocommerce_process_product_meta action and hooks the handler.
/* Save custom field data for the product */
function woocommerce_product_custom_fields_save($post_id)
{ $woocommerce_select_color_field = $_POST['_select_color']; if (!empty($woocommerce_select_color_field)) { update_post_meta($post_id, '_select_color', esc_attr($woocommerce_select_color_field)); }
}
add_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save');
3. Display an input field in a product single page
In this section, it prepares an add-on field HTML if the product meta is not empty.
First, the admin chooses and saves the product color option. Then this code will add a dropdown field on the product single page.
It reads the current product meta with the reference of the global post object. Then, it fetches the product meta by the custom field id.
In the WooCommerce product page, this custom field is added before the cart button. It is because of hooking the action woocommerce_before_add_to_cart_button to call this handler.
/* Show add-on field based on the saved custom field */
function woocommerce_display_select_option_value()
{ global $post; $product = wc_get_product($post->ID); $select_option_title_field = $product->get_meta('_select_color'); if ($select_option_title_field) { printf('
<div><select name="color_option" class="input-text text">
<option value="Default Color">Default Color</option>
<option value="' . $select_option_title_field . '">' . $select_option_title_field . '</option> </select></div>', esc_html($select_option_title_field)); }
}
add_action('woocommerce_before_add_to_cart_button', 'woocommerce_display_select_option_value');
4. Display the selected option in the cart and checkout pages
The following filter hooks handle the functions to display the selected custom option. It has the cart and checkout pages as its target to render the UI component.
This code is triggered on loading the cart item data. It calls the filter hook woocommerce_add_cart_item_data and displays the selected custom field value.
It adds the data into the cart item array which will be later used in the checkout page UI.
/* Display selected option in the cart */
function woocommerce_add_custom_field_item_data($cart_item_data, $product_id)
{ if (! empty($_POST['color_option'])) { $cart_item_data['select_field'] = $_POST['color_option']; } return $cart_item_data;
}
add_filter('woocommerce_add_cart_item_data', 'woocommerce_add_custom_field_item_data', 10, 2);
This is to parse the cart item array and display the custom field value on the checkout page.
It uses the woocommerce_cart_item_name hook to do this change in the checkout UI.
/* Display selected option in the checkout */
function woocommerce_cart_display($name, $cart_item, $cart_item_key)
{ if (isset($cart_item['select_field'])) { $name .= sprintf('<p>%s</p>', esc_html($cart_item['select_field'])); } return $name;
}
add_filter('woocommerce_cart_item_name', 'woocommerce_cart_display', 10, 3);
Conclusion
Thus, we have seen both methods to add custom fields to products. It is with or without plugins to enable WooCommerce product custom fields.
I hope, this article gives a basic knowledge in this area. Also, it might help you to understand and replicate the steps to customize your shop.
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 of the axis. It can also execute lambda functions. Read on for examples.ย ย ย ย
We will use a dataset of FIFA players. Find the dataset here.
Basic Setup using Jupyter Notebook
Letโs start by importing pandas and loading our dataset.
import pandas as pd
df_fifa_soccer_players = pd.read_csv('fifa_cleaned.csv')
df_fifa_soccer_players.head()
To increase readability, we will work with a subset of the data. Let’s create the subset by selecting the columns we want to have in our subset and create a new dataframe.
Pandas provides a variety of built-in aggregation functions. For example, pandas.DataFrame.describe. When applied to a dataset, it returns a summary of statistical values.ย
df_fifa_soccer_players_subset.describe()
To understand aggregation and why it is helpful, let’s have a closer look at the data returned.
Example: Our dataset contains records for 17954 players. The youngest player is 17 years of age and the oldest player is 46 years old. The mean age is 25 years. We learn that the tallest player is 205 cm tall and the average player’s height is around 175 cm. With a single line of code, we can answer a variety of statistical questions about our data. The describe function identifies numeric columns and performs the statistical aggregation for us. Describe also excluded the column nationality that contains string values.
To aggregate is to summarize many observations into a single value that represents a certain aspect of the observed data.
Pandas provides us with a variety of pre-built aggregate functions.
returns the count of unique values of a set of values
Let’s use another function from the list above. We can be more specific and request the โsumโ for the โvalue_euroโ series. This column contains the market value of a player. We select the column or series โvalue_euroโ and execute the pre-build sum() function.
Pandas returned us the requested value. Let’s get to know an even more powerful pandas method for aggregating data.
The โpandas.DataFrame.aggโ Method
Function Syntax
The .agg() function can take in many input types. The output type is, to a large extent, determined by the input type. We can pass in many parameters to the .agg() function.ย
The โfuncโ parameter:
is by default set to Noneย
contains one or many functions that aggregate the data
is by default set to 0 and applies functions to each column
if set to 1 applies functions to rows
can hold values:
0 or โindexโ
1 or โcolumnsโ
What about *args and **kwargs:
we use these placeholders, if we do not know in advance how many arguments we will need to pass into the function
when arguments are of the same type, we use *args
When arguments are of different types, we use **kwargs.
Agg method on a Series
Letโs see the .agg() function in action. We request some of the pre-build aggregation functions for the โwage_euroโ series. We use the function parameter and provide the aggregate functions โwe want to execute as a list. And letโs save the resulting series in a variable.ย
Pandas uses scientific notation for large and small floating-point numbers. To convert the output to a familiar format, we must move the floating point to the right as shown by the plus sign. The number behind the plus sign represents the amount of steps.
Let’s do this together for some values.
The sum of all wages is 175,347,000โฌ (1.753470e+08)
The mean of the wages is 9902.135โฌ (9.902135e+03)
We executed many functions on a series input source. Thus our variable โwage_statsโ is of the type Series because.ย
type(wage_stats)
# pandas.core.series.Series
See below how to extract, for example, the โminโ value from the variable and the data type returned.
Letโs use one more example to understand the relation between the input type and the output type.
We will use the function โnuniqueโ which will give us the count of unique nationalities. Letโs apply the function in two code examples. We will reference the series โnationalityโ both times. The only difference will be the way we pass the function โnuniqueโ into our agg() function.
When we use a dictionary to pass in the โnuniqueโ function, the output type is a series.
nationality_unique_int = df_fifa_soccer_players_subset['nationality'].agg('nunique')
print(nationality_unique_int)
# 160 print(type(nationality_unique_int))
# int
When we pass the โnuniqueโ function directly into agg() the output type is an integer.
Agg method on a DataFrame
Passing the aggregation functions as a Python list
One column represents a series. We will now select two columns as our input and so work with a dataframe.
Letโs select the columns โheight_cmโ and โweight_kgsโ.
We will execute the functions min(), mean() and max(). To select a two-dimensional data (dataframe), we need to use double brackets. We will round the results to two decimal points.
We will now use our newly created dataframe named โheight_weightโ to use the โaxisโ parameter. The entire dataframe contains numeric values.
We define the functions and pass in the axis parameter. I used the count() and sum() functions to show the effect of the axis parameter. The resulting values make little sense. This is also the reason why I do not rename the headings to restore the lost column names.
height_weight.agg(['count', 'sum'], axis=1)
We aggregated along the rows. Returning the count of items and the sum of item values in each row.
Passing the aggregation functions as a python dictionary
Now let’s apply different functions to the individual sets in our dataframe. We select the sets โoverall_ratingโ and โvalue_euroโ. We will apply the functions std(), sem() and mean() to the โoverall_ratingโ series, and the functions min() and max() to the โvalue_euroโ series.
Passing the aggregation functions as a Python tuple
We will now repeat the previous example.
We will use tuples instead of a dictionary to pass in the aggregation functions. Tuple have limitations. We can only pass one aggregation function within a tuple. We also have to name each tuple.ย
The โgroupbyโ method creates a grouped dataframe. We will now select the columns โageโ and โwage_euroโ and group our dataframe using the column โageโ. On our grouped dataframe we will apply the agg() function using the functions count(), min(), max() and mean().
Every row represents an age group. The count value shows how many players fall into the age group. The min, max and mean values aggregate the data of the age-group members.
Multiindex
One additional aspect of a grouped dataframe is the resulting hierarchical index. We also call it multiindex.
We can see that the individual columns of our grouped dataframe are at different levels. Another way to view the hierarchy is to request the columns for the particular dataset.
print(age_group_wage_euro.columns)
Working with a multiindex is a topic for another blog post. To use the tools that we have discussed, let’s flatten the multiindex and reset the index. We need the following functions:
The resulting dataframe columns are now flat. We lost some information during the flattening process. Letโs rename the columns and return some of the lost context.
Grouping by multiple columns creates even more granular subsections.
Letโs use โageโ as the first grouping parameter and โnationalityโ as the second. We will aggregate the resulting group data using the columns โoverall_ratingโ and โheight_cmโ. We are by now familiar with the aggregation functions used in this example.
Every age group contains nationality groups. The aggregated athletes data is within the nationality groups.
Custom aggregation functions
We can write and execute custom aggregation functions to answer very specific questions.
Letโs have a look at the inline lambda functions.
Lambda functions are so-called anonymous functions. They are called this way because they do not have a name. Within a lambda function, we can execute multiple expressions. We will go through several examples to see lambda functions in action.
In pandas lambda functions live inside the โDataFrame.apply()โ and the โSeries.appy()โ methods. We will use the DataFrame.appy() method to execute functions along both axes. Letโs have a look at the basics first.
Function Syntax
The DataFrame.apply() function will execute a function along defined axes of a DataFrame. The functions that we will execute in our examples will work with Series objects passed into our custom functions by the apply() method. Depending on the axes that we will select, the Series will comprise out of a row or a column or our data frame.
The โfuncโ parameter:
contains a function applied to a column or a row of the data frame
The โaxisโ parameter:
is by default set to 0 and will pass a series of column data
if set to 1 will pass a series of the row data
can hold values:
0 or โindexโ
1 or โcolumnsโ
The โrawโ parameter:
is a boolean value
ย is by default set toFalse
can hold values:
False-> a Series object is passed to the function
True -> a ndarray object is passed to the function
The โresult_typeโ parameter:
can only apply when the axis is 1 or โcolumnsโ
can hold values:
โexpandโ
โreduceโ
โbroadcastโ
ย The โargs()โ parameter:
additional parameters for the function as tuple
The **kwargs parameter:
additional parameters for the function as key-value pairs
Filters
Letโs have a look at filters. They will be very handy as we explore our data.
In this code example, we create a filter named filt_rating. We select our dataframe and the column overall_rating. The condition >= 90 returns True if the value in the overall_rating column is 90 or above.
The result is a Series object containing the index, and the correlated value of True or False.
Letโs apply the filter to our dataframe. We call the .loc method and pass in the filter’s name as a list item. The filter works like a mask. It covers all rows that have the value False. The remaining rows match our filter criteria of overall_rating >= 90.
df_fifa_soccer_players_subset.loc[filt_rating]
Lambda functions
Letโs recreate the same filter using a lambda function. We will call our filter filt_rating_lambda.
Letโs go over the code. We specify the name of our filter and call our dataframe. Pay attention to the double square brackets. We use them to pass a dataframe and not a Series object to the .appy() method.
Inside .apply() we use the keyword โlambdaโ to show that we are about to define our anonymous function. The โxโ represents the Series passed into the lambda function.
The series contains the data from the overall_rating column. After the semicolumn, we use the placeholder x again. Now we apply a method called ge(). It represents the same condition we used in our first filter example โ>=โ (greater or equal).
We define the integer value 90 and close the brackets on our apply function. The result is a dataframe that contains an index and only one column of boolean values. To convert this dataframe to a Series we use the squeeze() method.
We now want to know how many players our filter returned. Letโs first do it without a lambda function and then use a lambda function to see the same result. We are counting the lines or records.
Great. Now letโs put us in a place where we actually need to use the apply() method and a lambda function. We want to use our filter on a grouped data-frame.
Letโs group by nationality to see the distribution of these amazing players. The output will contain all columns. This makes the code easier to read.
Pandas tells us in this error message that we can not use the โlocโ method on a grouped dataframe object.
Letโs now see how we can solve this problem by using a lambda function. Instead of using the โlocโ function on the grouped dataframe we use the apply() function. Inside the apply() function we define our lambda function. Now we use the โlocโ method on the variable โxโ and pass our filter.ย
Now letโs use the axis parameter to calculate the Body-Mass-Index (BMI) for these players. Until now we have used the lambda functions on the columns of our data.
The โxโ variable was a representation of the individual column. We set the axis parameter to โ1โ. The โxโ variable in our lambda function will now represent the individual rows of our data.
Before we calculate the BMI letโs create a new dataframe and define some columns. We will call our new dataframe โdf_bmiโ.ย
We calculate the BMI as follows. We divide the weight in kilogram by the square of the height in meters.
Let’s have a closer look at the lambda function. We define the โaxisโ to be โ1โ. The โxโ variable now represents a row. We need to use specific values in each row. To define these values, we use the variable โxโ and specify a column name. At the beginning of our code example, we define a new column named โbmiโ. And at the very end, we round the results.
Great! Our custom function worked. The new BMI column contains calculated values.
Conclusion
Congratulations on finishing the tutorial. I wish you many great and small insights for your future data projects. I include the Jupyter-Notebook file, so you can experiment and tweak the code.
Nerd Humor
Oh yeah, I didn’t even know they renamed it the Willis Tower in 2009, because I know a normal amount about skyscrapers. — xkcd (source)
Mobile app development is a massive skill in the 21st century. In fact, the revenue of the mobile app market worldwide is mobile app revenue in 2022 is $437 billion USD. (Statista)
A mobile app developer is a programmer who focuses on software creation for mobile devices such as smartphones or wearables.
Most mobile app developers create smartphone apps for the Android, macOS, or Windows mobile operating system.
This article will show you the five areas of focus you could pursue to become a mobile app developer.
#1 – Android App Developer
An Android app developer is a programmer who focuses on software creation for mobile devices such as smartphones or wearables using the Android operating system.
How much does an Android App Developer make per year?
Figure: Average Income of an Android App Developer in the US by Source.
The average annual income of an Android App Developer in the United States is between $85,000 and $126,577 with an average of $106,923 and a statistical median of $107,343 per year.
Learn More: I’ve written a full guide on this career path and published it on the Finxter blog here.
#2 – iOS App Developer
An iOS app developer is a programmer who focuses on software creation for Apple mobile devices such as iPhones or wearables such as Apple Watches. Most mobile app developers create smartphone apps for the iOS or watchOS mobile operating systems using the Swift programming language.
How much does an iOS App Developer make per year?
The average annual income of an iOS Developer in the United States is between $83,351 and $145,000 with an average of $110,331 and a statistical median of $111,716 per year.
Learn More: I’ve written a full guide on this career path and published it on the Finxter blog here.
#3 – Firebase Developer
Firebase is a Google-based platform to create mobile and web applications easily. Firebase developers are programmers who create mobile apps with Firebase
The average annual income of a Firebase Developer is approximately $80,000 according to PayScale (source).
Learn More: I’ve written a full guide on this career path and published it on the Finxter blog here.
#4 – Flutter Developer
A Flutter Developer developer creates, edits, analyzes, debugs, and supervises the development of Android mobile apps written in the Flutter programming framework using the Dart programming language.
The average income of a Flutter developer in the US is $112,125 per year or $57.50 per hour. Entry-level Flutter developers start with approximately $100,000 per year. Experienced developers make up to $159,900 per year. (source)
Learn More: I’ve written a full guide on this career path and published it on the Finxter blog here.
#5 – Kotlin Developer
A Kotlin Developer is an Android app programmer using the Kotlin programming language.
Kotlin is JVM compatible and, thus, fully compatible with Java. Thatโs why itโs often used as an alternative to Java when developing Android applications.
The average annual income of a Kotlin Developer is $102,000 according to PayScale and averages between $113,000 to $147,000 per year according to Ziprecruiter.
Learn More: I’ve written a full guide on this career path and published it on the Finxter blog here.
#6 – Swift Developer
A Swift developer is a programmer who creates software and mobile applications for the Swift programming language for iOS, iPadOS, macOS, tvOS, and watchOS. (Source)
The average annual income of a Swift Developer is between $93,000 (25th percentile) and $114,500 (75th percentile) according to Ziprecruiter (source).
Learn More: I’ve written a full guide on this career path and published it on the Finxter blog here.
Bonus #7 – Alexa Developer
Alexa is the cloud-based voice service distributed and sold by Amazon. It is available on hundreds of millions of devices and from third-party device manufacturers.
The average income of an Alexa skill developer in the US is $88,617 per year according to ZipRecruiter. This is approximately $42 per hour, $1,704 per week, or $7,385 per month.
Conclusion
A mobile app developer is a programmer who focuses on software creation for mobile devices such as smartphones or wearables. Most mobile app developers create smartphone apps for the Android, macOS, or Windows mobile operating system.
This article has shown you six main areas of focus. The technologies presented here overlap significantly but I hope reading this article has given you an initial glimpse into the world of mobile app development.
We start with exploring this basic challenge and build from there by changing the delimiter and using Pandas to access individual columns.
But first things first: How to convert a CSV file to a TXT file without changing its contents?
Method 1: CSV to TXT Unchanged
If you want to keep the content (including the delimiter ',') in the CSV file unmodified, the conversion is simple: read the .csv file and write its content into a new .txt file using the open(), read(), and write() functions without importing any library.
In other words, perform the three steps to write a CSV to a TXT file unmodified:
Open the CSV file in reading mode and the TXT file in writing mode.
Read the CSV file and store it in a variable.
Write the content into the TXT file.
Here’s the code snippet that solves our basic challenge:
# 1. Open the CSV file in reading mode and the TXT file in writing mode
with open('my_file.csv', 'r') as f_in, open('my_file.txt', 'w') as f_out: # 2. Read the CSV file and store in variable content = f_in.read() # 3. Write the content into the TXT file f_out.write(content)
Little-Known Fact: Python allows multiple expressions in the context manager (with opening line) if you separate them with a comma.
The content of the .csv and .txt files is identical:
Name Job Age Income
Alice Programmer 23 110000
Bob Executive 34 90000
Carl Sales 45 50000
Here’s the simple solution to this challenge:
If you want to change the delimiter ',' to an empty string ' ' in the new TXT file, read the .csv file and write its content into a new .txt file using the open(), read(), string.replace(), and write() functions without importing any library.
To convert a CSV to a TXT file in Python, perform the following steps:
Open the CSV file in reading mode and the TXT file in writing mode.
Read the CSV file into a string.
Create a new string by replacing all occurrences of the delimiter ',' with the empty string ' '.
Write the content into the TXT file.
with open('my_file.csv', 'r') as f_in, open('my_file.txt', 'w') as f_out: content = f_in.read().replace(',', ' ') f_out.write(content)
So far, so good. But in Python, there are always many ways to solve a problem. Let’s have a look at a powerful alternative to the no-library approach used before:
Method 3: CSV to TXT using Pandas
Assuming you’ve already installed pandas in your local environment, you can write a CSV to a TXT file in Python pandas using the following four steps:
Little-Known Fact: Python’s print() function allows you to write a string directly into a file object if you use the file argument as shown in the code snippet.
The output of the previous code snippet is as follows:
Name Job Age Income
0 Alice Programmer 23 110000
1 Bob Executive 34 90000
2 Carl Sales 45 50000
Beautiful, isn’t it?
Let’s have a look at the last variation of the “CSV to TXT” problem addressed in this tutorial:
Method 4: CSV Columns or Rows to TXT using Pandas
How to write one or more individual columns or rows of the CSV file into a TXT file using Python Pandas?
The content of the new file 'my_file.txt' shows that only the first two rows have been taken due to the slicing operation [:2] in the previous code snippet:
0 Alice
1 Bob
Done! You’ve earned some programming enjoyment:
Programmer Humor
Question: How did the programmer die in the shower? ☠
❗ Answer: They read the shampoo bottle instructions: Lather. Rinse. Repeat.
Conclusion
I hope you enjoyed reading this article and learned something new. Feel free to join our email newsletter with free cheat sheets and weekly Python tutorials: