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
Programming Articles - Page 1055 of 3366
795 Views
We will be using the iat attribute to access the last element, since it is used to access a single value for a row/column pair by integer position.Let us first import the required Pandas library −import pandas as pdCreate a Pandas series with numbers −data = pd.Series([10, 20, 5, 65, 75, 85, 30, 100])Now, get the last element using iat() −data.iat[-1]ExampleFollowing is the code −import pandas as pd # pandas series data = pd.Series([10, 20, 5, 65, 75, 85, 30, 100]) print"Series...", data # get the first element print"The first element in the series = ", data.iat[0] ... Read More
230 Views
When it is required to count the frequency of the matrix row length, it is iterated over and its frequency is added to the empty dictionary or incremented if found again.ExampleBelow is a demonstration of the samemy_list = [[42, 24, 11], [67, 18], [20], [54, 10, 25], [45, 99]] print("The list is :") print(my_list) my_result = dict() for element in my_list: if len(element) not in my_result: my_result[len(element)] = 1 else: my_result[len(element)] += 1 print("The result is :") print(my_result)OutputThe ... Read More
791 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
209 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
161 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
163 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
229 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
324 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
332 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