
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

184 Views
To fetch only capital words, we are using regex. The re module is used here and imported. Let us import all the libraries −import re import pandas as pdCreate a DataFrame −data = [['computer', 'mobile phone', 'ELECTRONICS', 'electronics'], ['KEYBOARD', 'charger', 'SMARTTV', 'camera']] dataFrame = pd.DataFrame(data)Now, extract capital words −for i in range(dataFrame.shape[1]): for ele in dataFrame[i]: if bool(re.match(r'\w*[A-Z]\w*', str(ele))): print(ele)ExampleFollowing is the code −import re import pandas as pd # create a dataframe data = [['computer', 'mobile phone', 'ELECTRONICS', 'electronics'], ... Read More

313 Views
Use the isin() method to display True for infinite values. At first, let us import the required libraries with their respective aliases −import pandas as pd import numpy as npCreate a dictionary of list. We have set the infinity values using the Numpy np.inf −d = { "Reg_Price": [7000.5057, np.inf, 5000, np.inf, 9000.75768, 6000, 900, np.inf] } Creating DataFrame from the above dictionary of list −dataFrame = pd.DataFrame(d)Display True for infinite values −res = dataFrame.isin([np.inf, -np.inf]) ExampleFollowing is the code −import pandas as pd import numpy as np # dictionary of list d = { "Reg_Price": [7000.5057, np.inf, 5000, ... Read More

1K+ Views
To check and display row index, use the isinf() with any(). At first, let us import the required libraries with their respective aliases −import pandas as pd import numpy as npCreate a dictionary of list. We have set the infinity values using the Numpy np.inf −d = { "Reg_Price": [7000.5057, np.inf, 5000, np.inf, 9000.75768, 6000, 900, np.inf] } Creating DataFrame from the above dictionary of list −dataFrame = pd.DataFrame(d)Getting row index with infinity values −indexNum = dataFrame.index[np.isinf(dataFrame).any(1)] ExampleFollowing is the code −import pandas as pd import numpy as np # dictionary of list d = { "Reg_Price": [7000.5057, np.inf, ... Read More

1K+ Views
To count the observations, first use the groupby() and then use count() on the result. At first, import the required library −dataFrame = pd.DataFrame({'Product Name': ['Keyboard', 'Charger', 'SmartTV', 'Camera', 'Graphic Card', 'Earphone'], 'Product Category': ['Computer', 'Mobile Phone', 'Electronics', 'Electronics', 'Computer', 'Mobile Phone'], 'Quantity': [10, 50, 10, 20, 25, 50]})Group the column with duplicate values −group = dataFrame.groupby("Product Category") Get the count −group.count()ExampleFollowing is the code −import pandas as pd # create a dataframe dataFrame = pd.DataFrame({'Product Name': ['Keyboard', 'Charger', 'SmartTV', 'Camera', 'Graphic Card', 'Earphone'], 'Product Category': ['Computer', 'Mobile Phone', 'Electronics', 'Electronics', 'Computer', 'Mobile Phone'], 'Quantity': [10, 50, 10, 20, ... Read More

858 Views
To sort data in ascending or descending order, use sort_values() method. For ascending order, use the following is the sort_values() method −ascending=TrueImport the required library −import pandas as pd Create a DataFrame with 3 columns −dataFrame = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'BMW', 'Mustang', 'Mercedes', 'Lexus'], "Reg_Price": [7000, 1500, 5000, 8000, 9000, 2000], "Place": ['Pune', 'Delhi', 'Mumbai', 'Hyderabad', 'Bangalore', 'Chandigarh'] } ) To sort DataFrame in ascending order according to the element frequency, we need to count the occurrences. Therefore, count() is also used with sort_values() set for asscending order sort −dataFrame.groupby(['Car'])['Reg_Price'].count().reset_index(name='Count').sort_values(['Count'], ... Read More

527 Views
To get the maximum of column values, use the max() function. At first, import the required Pandas library −import pandas as pdNow, create a DataFrame with two columns −dataFrame1 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Tesla', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] } )Finding the maximum value of a single column “Units” using max() −print"Maximum Units from DataFrame1 = ", dataFrame1['Units'].max()In the same way, we have calculated the maximum value from the 2nd DataFrame.ExampleFollowing is the complete code −import pandas as pd # Create DataFrame1 dataFrame1 = pd.DataFrame( { ... Read More

4K+ Views
Use index=False to ignore index. Let us first import the required library −import pandas as pdCreate a DataFrame −dataFrame = pd.DataFrame([[10, 15], [20, 25], [30, 35]], index=['x', 'y', 'z'], columns=['a', 'b'])Select rows by passing label using loc −dataFrame.loc['x'] Display DataFrame without index −dataFrame.to_string(index=False)ExampleFollowing is the code −import pandas as pd # Create DataFrame dataFrame = pd.DataFrame([[10, 15], [20, 25], [30, 35]], index=['x', 'y', 'z'], columns=['a', 'b']) # DataFrame print"Displaying DataFrame with index...", dataFrame # select rows with loc print"Select rows by passing label..." print(dataFrame.loc['x']) # display DataFrame without index print"Displaying DataFrame without Index...", dataFrame.to_string(index=False)OutputThis will produce ... Read More

466 Views
When it is required to sort the palindrome words present in a sentence, a method is defined that takes a string as a parameter and first ensures that it is a palindrome. Then it sorts all the words of a string and returns it as output.ExampleBelow is a demonstration of the samedef check_palindrome(my_string): if(my_string == my_string[::-1]): return True else: return False def print_sort_palindromes(my_sentence): my_list = [] my_result = list(my_sentence.split()) for ... Read More

867 Views
When it is required to generate all possible permutations of a word in a sentence, a function is defined. This function iterates over the string and depending on the condition, the output is displayed.ExampleBelow is a demonstration of the samefrom itertools import permutations def calculate_permutations(my_string): my_list = list(my_string.split()) permutes = permutations(my_list) for i in permutes: permute_list = list(i) for j in permute_list: print j print() my_string = "hi there" print("The string is :") print(my_string) ... Read More

186 Views
When it is required to extract rows from a matrix with different data types, it is iterated over and ‘set’ is used to get the distinct types.ExampleBelow is a demonstration of the samemy_list = [[4, 2, 6], ["python", 2, {6: 2}], [3, 1, "fun"], [9, (4, 3)]] print("The list is :") print(my_list) my_result = [] for sub in my_list: type_size = len(list(set([type(ele) for ele in sub]))) if len(sub) == type_size: my_result.append(sub) print("The resultant distinct data type rows are :") print(my_result)OutputThe list is : [[4, ... Read More