
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 26504 Articles for Server Side Programming

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

284 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

156 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

536 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

270 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

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

410 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

484 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

284 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

568 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