How to Export Pandas Series to a CSV File

Here are 4 approaches to export Pandas Series to a CSV file: (1) No index and no header Copy import pandas as pdser = pd.Series([“value_1”, “value_2”, “value_3″, …])ser.to_csv(r”Path to store the CSV file\File Name.csv”, index=False, header=False) (2) No index but with header Copy import pandas as pdser = pd.Series([“value_1”, “value_2”, “value_3”, …])ser.rename(“header name”, inplace=True)ser.to_csv(r”Path to … Read more

Iterate over a List of Lists in Python

The following syntax can be used to iterate over a list of lists: Copy my_list = [[“a”, “b”, “c”], [“d”, “e”, “f”], [“g”, “h”, “i”]]for x in my_list: for y in x: print(y) Example of iterating over a list of lists Let’s suppose that you have a list of lists of colors. You can then … Read more

How to Iterate over a Dictionary in Python

Here are 3 ways to iterate over a dictionary in Python: (1) Iterate over the keys of a dictionary: Copy for key in my_dict.keys(): print(key) (2) Iterate over the values of a dictionary: Copy for value in my_dict.values(): print(value) (3) Iterate over both the keys and values of a dictionary: Copy for key, value in … Read more

Flatten a List of Lists in Python

Here are two ways to flatten a list of lists in Python: (1) Using a List Comprehension: Copy flatten_list = [i for s in list_of_lists for i in s] (2) Using a For Loop: Copy flatten_list = []for s in list_of_lists: for i in s: flatten_list.append(i) Examples Example 1: Flatten a list of lists in … Read more

Remove Empty Strings from Python List

Here are 4 ways to remove empty strings from a list in Python: (1) Using a List Comprehension: Copy new_list = [x for x in list_with_empty_strings if x != “”] (2) Using a For Loop: Copy new_list = []for x in list_with_empty_strings: if x != “”: new_list.append(x) (3) Using Filter: Copy new_list = list(filter(None, list_with_empty_strings)) … Read more

Convert two Lists into a Dictionary in Python

Here are two ways to convert two lists into a dictionary in Python: (1) Using zip() and dict(): Copy list_keys = [“item_1”, “item_2”, “item_3”, …]list_values = [“item_1”, “item_2”, “item_3”, …]new_dict = dict(zip(list_keys, list_values)) (2) Using a Dictionary Comprehension: Copy list_keys = [“item_1”, “item_2”, “item_3”, …]list_values = [“item_1”, “item_2”, “item_3”, …]new_dict = {k: v for k, … Read more

How to Create a Dictionary in Python

Here are 2 ways to create a Dictionary in Python: (1) Using curly brackets {} Copy my_dictionary = {1: “aaa”, 2: “bbb”, 3: “ccc”} (2) Using the dict() function Copy my_dictionary = dict({1: “aaa”, 2: “bbb”, 3: “ccc”}) Note that each dictionary stores data in key: value pairs. In addition, the keys must be unique … Read more

4 Ways to Extract Dictionary Keys as a List

Here are 4 ways to extract dictionary keys as a list in Python: (1) Using a list() function: Copy my_list = list(my_dict) (2) Using dict.keys(): Copy my_list = list(my_dict.keys()) (3) Using List Comprehension: Copy my_list = [i for i in my_dict] (4) Using For Loop: Copy my_list = [] for i in my_dict: my_list.append(i) Examples … Read more