
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 10476 Articles for Python

779 Views
To rename multiple column headers, use the rename() method and set the dictionary in the columns parameter. At first, let us create a DataFrame −dataFrame = pd.DataFrame({"Car": ['BMW', 'Mustang', 'Tesla', 'Mustang', 'Mercedes', 'Tesla', 'Audi'], "Cubic Capacity": [2000, 1800, 1500, 2500, 2200, 3000, 2000], "Reg Price": [7000, 1500, 5000, 8000, 9000, 6000, 1500], "Units Sold": [ 200, 120, 150, 120, 210, 250, 220] })Creating a dictionary to rename columns. The key and value pairs as old name and new name −dictionary = {'Car': 'Car Name', 'Cubic Capacity': 'CC', 'Reg Price': 'Registration Price', 'Units Sold': 'Units Purchased' }Use rename() and set the ... Read More

199 Views
To select columns with specific datatypes, use the select_dtypes() method and the include parameter. At first, create a DataFrame with 2 columns −dataFrame = pd.DataFrame( { "Student": ['Jack', 'Robin', 'Ted', 'Marc', 'Scarlett', 'Kat', 'John'], "Roll Number": [ 5, 10, 3, 8, 2, 9, 6] } )Now, select the 2 columns with their respective specific datatype −column1 = dataFrame.select_dtypes(include=['object']).columns column2 = dataFrame.select_dtypes(include=['int64']).columnsExampleFollowing is the code −import pandas as pd # Create DataFrame dataFrame = pd.DataFrame( { "Student": ['Jack', 'Robin', 'Ted', 'Marc', 'Scarlett', 'Kat', 'John'], "Roll Number": [ 5, 10, ... Read More

153 Views
To get the datatypes of columns, use the info() method. Let us first import the required library −import pandas as pdCreate a DataFrame with 2 columns having different datatypes −dataFrame = pd.DataFrame( { "Student": ['Jack', 'Robin', 'Ted', 'Marc', 'Scarlett', 'Kat', 'John'], "Roll Number": [ 5, 10, 3, 8, 2, 9, 6] } )Get the complete information about datatypes −dataFrame.info()ExampleFollowing is the complete code −import pandas as pd # Create DataFrame dataFrame = pd.DataFrame( { "Student": ['Jack', 'Robin', 'Ted', 'Marc', 'Scarlett', 'Kat', 'John'], "Roll Number": [ 5, 10, 3, ... Read More

160 Views
When it is required to get the character repetitions of a given character, a method is defined that uses the index value to print the repetitions.ExampleBelow is a demonstration of the samedef to_string(my_list): return ''.join(my_list) def lex_recurrence(my_string, my_data, last_val, index_val): length = len(my_string) for i in range(length): my_data[index_val] = my_string[i] if index_val==last_val: print(to_string(my_data)) else: ... Read More

219 Views
When it is required to group the contiguous elements of a string that are present in a list, a method is defined that uses ‘groupby’, and ‘yield’.ExampleBelow is a demonstration of the samefrom itertools import groupby def string_check(elem): return isinstance(elem, str) def group_string(my_list): for key, grp in groupby(my_list, key=string_check): if key: yield list(grp) else: yield from ... Read More

2K+ Views
To add a zero column to a Pandas DataFrame, use the square bracket and set it to 0. At first, import te required library −import pandas as pdCreate 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] } )Create a new column with zero entries −dataFrame['ZeroColumn'] = 0 ExampleFollowing is the code −import pandas as pd # Create DataFrame dataFrame = pd.DataFrame( { ... Read More

315 Views
When it is required to find the element frequencies in the percentage range, the ‘Counter’ is used along with a simple iteration technique.ExampleBelow is a demonstration of the samefrom collections import Counter my_list = [56, 34, 78, 90, 11, 23, 6, 56, 79, 90] print("The list is :") print(my_list) start, end = 13, 60 my_freq = dict(Counter(my_list)) my_result = [] for element in set(my_list): percent = (my_freq[element] / len(my_list)) * 100 if percent >= start and percent

324 Views
When it is required to get the specific number of power of a number, the ‘**’ operator is used along with list comprehension.ExampleBelow is a demonstration of the samen = 4 print("The value n is : ") print(n) k = 5 print("The value of k is : ") print(k) result = [n ** index for index in range(0, k)] print("The square values of N till K : " ) print(result)OutputThe value n is : 4 The value of k is : 5 The square values of N till K : [1, 4, 16, 64, 256]ExplanationThe values for ‘n’ ... Read More

359 Views
When it is required to find duplicate sets in a list of sets, the ‘Counter’ and ‘frozenset’ are used.ExampleBelow is a demonstration of the samefrom collections import Counter my_list = [{4, 8, 6, 1}, {6, 4, 1, 8}, {1, 2, 6, 2}, {1, 4, 2}, {7, 8, 9}] print("The list is :") print(my_list) my_freq = Counter(frozenset(sub) for sub in my_list) my_result = [] for key, value in my_freq.items(): if value > 1 : my_result.append(key) print("The result is :") print(my_result)OutputThe list is : [{8, 1, 4, 6}, {8, 1, ... Read More

299 Views
When it is required to add custom borders to the matrix, a simple list iteration can be used to add the required borders to the matrix.ExampleBelow is a demonstration of the samemy_list = [[2, 5, 5], [2, 7, 5], [4, 5, 1], [1, 6, 6]] print("The list is :") print(my_list) print("The resultant matrix is :") border = "|" for sub in my_list: my_temp = border + " " for ele in sub: my_temp = my_temp + str(ele) + " " ... Read More