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

810 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

305 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

563 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

Find Distance Between First and Last Even Elements in a List

AmitDiwan
Updated on 20-Sep-2021 06:48:21

441 Views

When it is required to find the distance between the first and last even elements of a list, list elements are accessed using indexing and the difference is found.ExampleBelow is a demonstration of the samemy_list = [2, 3, 6, 4, 6, 2, 9, 1, 14, 11] print("The list is :") print(my_list) my_indices_list = [idx for idx in range( len(my_list)) if my_list[idx] % 2 == 0] my_result = my_indices_list[-1] - my_indices_list[0] print("The result is :") print(my_result)OutputThe list is : [2, 3, 6, 4, 6, 2, 9, 1, 14, 11] The result is : 8ExplanationA list ... Read More

Filter Rows with Only Alphabets from List of Lists in Python

AmitDiwan
Updated on 20-Sep-2021 06:45:59

509 Views

When it is required to filter the rows that contains only alphabets in a list of lists, the list is iterated over and the ‘isalpha’ method is used to check if an alphabet is present or not.ExampleBelow is a demonstration of the samemy_list = [["python", "is", "best"], ["abc123", "good"], ["abc def ghij"], ["abc2", "gpqr"]] print("The list is :") print(my_list) my_result = [sub for sub in my_list if all(element.isalpha() for element in sub)] print("The result is :") print(my_result)OutputThe list is : [['python', 'is', 'best'], ['abc123', 'good'], ['abc def ghij'], ['abc2', 'gpqr']] The result is : [['python', 'is', 'best']]ExplanationA list ... Read More

Find the Sum of Length of Strings at Given Indices in Python

AmitDiwan
Updated on 20-Sep-2021 06:43:55

313 Views

When it is required to find the sum of the length of string at specific indices, the ‘enumerate’ is used to iterate through the elements in the list and adding the length of the element to a list.ExampleBelow is a demonstration of the samemy_list = ["python", "is", "best", "for", "coders"] print("The list is :") print(my_list) index_list = [0, 1, 4] result = 0 for index, element in enumerate(my_list): if index in index_list: result += len(element) print("The result is :") print(result)OutputThe list is : ['python', 'is', 'best', 'for', ... Read More

Test If Elements of List Are in Min-Max Range from Another List in Python

AmitDiwan
Updated on 20-Sep-2021 06:42:12

593 Views

When it is required to test if the elements are in the min/max range, the list elements are iterated over, and are checked to see if it is equal to ‘max’ value.ExampleBelow is a demonstration of the samemy_list = [5, 6, 4, 7, 8, 13, 15] print("The list is : ") print(my_list) range_list = [4, 7, 10, 6] my_result = True for elem in range_list: if elem!= max(my_list): my_result = False break if(elem == True): print("All the elements are ... Read More

Advertisements