Remove the First Rows in Pandas DataFrame

You may use the following syntax to remove the first row/s in Pandas DataFrame: (1) Remove the first row in a DataFrame: Copy df = df.iloc[1:] (2) Remove the first n rows in a DataFrame: Copy df = df.iloc[n:] Examples of Removing the First Rows in a DataFrame Example 1: Remove the first row 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

How to Extract Dictionary Values as a List

Here are 3 approaches to extract dictionary values as a list in Python: (1) Using a list() function: Copy my_list = list(my_dict.values()) (2) Using a List Comprehension: Copy my_list = [i for i in my_dict.values()] (3) Using For Loop: Copy my_list = [] for i in my_dict.values(): my_list.append(i) Next, you’ll see few examples of extracting … 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 = … 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 and … 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

Convert Python List to a NumPy Array

The following syntax can be used to convert a Python list to a numpy array: Copy my_array = np.array(my_list) In this guide, you’ll see how to convert: Python list to a numpy array List of lists (multi-dimensional list) to a numpy array (1) Convert Python List to a NumPy Array Let’s create a simple list … Read more

How to Convert NumPy Array to a List in Python

You can use tolist() in order to convert a numpy array to a list in Python: Copy my_list = my_array.tolist() Below are the steps to convert a numpy array to a list (as well as to a list of lists) using practical examples. Steps to Convert NumPy Array to a List in Python Step 1: … Read more

Replace NA Values with Zeros in DataFrame in R

Here are 2 ways to replace NA values with zeros in a DataFrame in R: (1) Replace NA values with zeros across the entire DataFrame: Copy df[is.na(df)] <- 0 (2) Replace NA values with zeros under a single DataFrame column: Copy df[“column_name”][is.na(df[“column_name”])] <- 0 In the following section, you’ll see how to apply the above … Read more

Categories R