Merge Dictionaries in Python with Duplicate Keys

AmitDiwan
Updated on 20-Sep-2021 07:56:21

775 Views

When it is required to merge dictionaries list with duplicate keys, the keys of the strings are iterated over and depending on the condition, the result is determined.ExampleBelow is a demonstration of the samemy_list_1 = [{"aba": 1, "best": 4}, {"python": 10, "fun": 15}, {"scala": "fun"}] my_list_2 = [{"scala": 6}, {"python": 3, "best": 10}, {"java": 1}] print("The first list is : ") print(my_list_1) print("The second list is : ") print(my_list_2) for i in range(0, len(my_list_1)):    id_keys = list(my_list_1[i].keys())    for key in my_list_2[i]:       if key not in id_keys: ... Read More

Remove Columns of Duplicate Elements in Python

AmitDiwan
Updated on 20-Sep-2021 07:51:52

164 Views

When it is required to remove columns of duplicate elements, a method is defined that creates an empty set. The list is iterated over, and if it is not found, it is added to the set.ExampleBelow is a demonstration of the samefrom itertools import chain def remove_dupes(my_sub):    my_string = set()    for i, elem in enumerate(my_sub):       if elem not in my_string:          my_string.add(elem)       else:          yield i my_list = [[5, 1, 6, 7, 9], [6, 3, 1, 9, 1], [4, 2, 9, 8, ... Read More

Move a Column to the First Position in Pandas DataFrame

AmitDiwan
Updated on 20-Sep-2021 07:48:45

1K+ Views

Use pop() to pop the column and insert it using the insert() methodi.e. moving a column. At first, create a DataFrame with 3 columns −dataFrame = pd.DataFrame(    {       "Student": ['Jack', 'Robin', 'Ted', 'Marc', 'Scarlett', 'Kat', 'John'], "Result": ['Pass', 'Fail', 'Pass', 'Fail', 'Pass', 'Pass', 'Pass'], "Roll Number": [ 5, 10, 3, 8, 2, 9, 6] } ) Move column "Roll Number" to 1st position by first popping the column out −shiftPos = dataFrame.pop("Roll Number")Insert column on the 1st position −dataFrame.insert(0, "Roll Number", shiftPos) ExampleFollowing is the code −import pandas as pd # ... Read More

Element-wise Matrix Difference in Python

AmitDiwan
Updated on 20-Sep-2021 07:48:25

280 Views

When it is required to print element wise matrix difference, the list elements are iterated over and the zip method is used on these values.ExampleBelow is a demonstration of the samemy_list_1 = [[3, 4, 4], [4, 3, 1], [4, 8, 3]] my_list_2 = [[5, 4, 7], [9, 7, 5], [4, 8, 4]] print("The first list is :") print(my_list_1) print("The second list is :") print(my_list_2) my_result = [] for sub_str_1, sub_str_2 in zip(my_list_1, my_list_2):    temp_str = []    for element_1, element_2 in zip(sub_str_1, sub_str_2):       temp_str.append(element_2-element_1)    my_result.append(temp_str) print("The result is :") print(my_result)OutputThe first ... Read More

Display Only Non-Duplicate Values from a DataFrame in Python

AmitDiwan
Updated on 20-Sep-2021 07:42:04

4K+ Views

We will see how to display only non-duplicated values. At first, we will create a DataFrame with duplicate values −dataFrame = pd.DataFrame(    {       "Student": ['Jack', 'Robin', 'Ted', 'Robin', 'Scarlett', 'Kat', 'Ted'], "Result": ['Pass', 'Fail', 'Pass', 'Fail', 'Pass', 'Pass', 'Pass'] } )Above, we have created 2 columns. To display only non-duplicated values, use the duplicated() method and logical NOT. Through this, non-duplicated values will be fetched −dataFrame[~dataFrame.duplicated('Student')] ExampleFollowing is the complete code −import pandas as pd # Create DataFrame dataFrame = pd.DataFrame(    {       "Student": ['Jack', 'Robin', 'Ted', 'Robin', 'Scarlett', ... Read More

Reshape Data in a Pandas DataFrame

AmitDiwan
Updated on 20-Sep-2021 07:34:11

789 Views

We can easily reshape the data by categorizing a specific column. Here, we will categorize the “Result”column i.e. Pass and Fail values in numbers form.Import the required library −import pandas as pdCreate a DataFrame with 2 columns −dataFrame = pd.DataFrame(    {       "Student": ['Jack', 'Robin', 'Ted', 'Scarlett', 'Kat'], "Result": ['Pass', 'Fail', 'Fail', 'Pass', 'Pass'] } )Reshape the data using the map() function and just set ‘Pass’ to 1 and ‘Fail’ to 0 −dataFrame['Result'] = dataFrame['Result'].map({'Pass': 1, 'Fail': 0, }) ExampleFollowing is the code − import pandas as pd # Create DataFrame dataFrame ... Read More

Convert Pandas DataFrame to Binary Data in Python

AmitDiwan
Updated on 20-Sep-2021 07:27:52

4K+ Views

Use the get_dummies() method to convert categorical DataFrame to binary data. Following is our Pandas DataFrame with 2 columns −dataFrame = pd.DataFrame(    {       "Student": ['Jack', 'Robin', 'Ted', 'Scarlett', 'Kat'], "Result": ['Pass', 'Fail', 'Fail', 'Pass', 'Pass'] } )Use the get_dummies() and set the column which you want to convert to binary form. Here, we want the Result in “Pass” and “Fail” form to be visible. Therefore, we will set the “Result” column −pd.get_dummies(dataFrame["Result"]ExampleFollowing is the code −import pandas as pd # Create DataFrame dataFrame = pd.DataFrame(    {       "Student": ['Jack', ... Read More

Renaming Column Names in Python Pandas

AmitDiwan
Updated on 20-Sep-2021 07:21:18

294 Views

We can use the rename() method to rename column names. Let’s say the following is our Pandas DataFrame with 3 columns −dataFrame = pd.DataFrame(    {       "Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], "Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000], "Units": [90, 120, 100, 150, 200, 130] } )We will rename two columns i.e. “Car” to “Car Names” and “Reg_Price” to “Registration Cost”:dataFrame.rename(columns={dataFrame.columns[0]: 'Car Names', dataFrame.columns[1]: 'Registration Cost'}) ExampleFollowing is the code − import pandas as pd # Create DataFrame dataFrame = pd.DataFrame(    {       "Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', ... Read More

Limit Values to Keys in a Dictionary List in Python

AmitDiwan
Updated on 20-Sep-2021 07:14:43

1K+ Views

When it is required to limit the values to keys in a list of dictionary, the keys are accessed and the ‘min’ and ‘max’ methods are used to limit the values.ExampleBelow is a demonstration of the samemy_list = [{"python": 4, "is": 7, "best": 10}, {"python": 2, "is": 5, "best": 9}, {"python": 1, "is": 2, "best": 6}] print("The list is :") print(my_list) my_result = dict() keys = list(my_list[0].keys()) for my_elem in keys:    my_result[my_elem] = [min(sub[my_elem] for sub in my_list), max(sub[my_elem] for sub in my_list)] print("The result is :") print(my_result)OutputThe list is : [{'python': 4, ... Read More

Rename Column Names by Index in a Pandas DataFrame

AmitDiwan
Updated on 20-Sep-2021 07:13:51

546 Views

We can easily rename a column by index i.e. without using rename(). Import the required library −import pandas as pdCreate a DataFrame with 3 columns −dataFrame = pd.DataFrame(    {       "Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], "Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000], "Units": [90, 120, 100, 150, 200, 130] } )Let us now rename all the columns using columns.values[0[ by setting the index of the column to be changed in the square brackets −dataFrame.columns.values[0] = "Car Names" dataFrame.columns.values[1] = "Registration Cost" dataFrame.columns.values[2] = "Units_Sold"ExampleFollowing is the code −import pandas as pd ... Read More

Advertisements