
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 33676 Articles for Programming

876 Views
To drop specific rows rom multiindex dataframe, use the drop() method. At first, let us create a multi-index array −arr = [np.array(['car', 'car', 'car', 'bike', 'bike', 'bike', 'truck', 'truck', 'truck']), np.array(['valueA', 'valueB', 'valueC', 'valueA', 'valueB', 'valueC', 'valueA', 'valueB', 'valueC'])]Next, create multiindex dataframe and set index also −dataFrame = pd.DataFrame( np.random.randn(9, 3), index=arr, columns=['Col 1', 'Col 2', 'Col 3']) dataFrame.index.names = ['level 0', 'level 1']Now, drop specific row −dataFrame.drop(('car', 'valueA'), axis=0, inplace=True)ExampleFollowing is the code −import numpy as np import pandas as pd # multiindex array arr = [np.array(['car', 'car', 'car', 'bike', 'bike', 'bike', 'truck', 'truck', 'truck']), ... Read More

1K+ Views
Let us see how to find the sum of negative and positive values. At first, create a dataframe with positive and negative values −dataFrame = pd.DataFrame({'Place': ['Chicago', 'Denver', 'Atlanta', 'Chicago', 'Dallas', 'Denver', 'Dallas', 'Atlanta'], 'Temperature': [-2, 30, -5, 10, 30, -5, 20, -10]})Next, use groupby to group on the basis of Place column −groupRes = dataFrame.groupby(dataFrame['Place'])Use lambda function to return the positive and negative values. We have also added the positive and negative values individually −# lambda function def plus(val): return val[val > 0].sum() def minus(val): return val[val < 0].sum()ExampleFollowing is the complete code ... Read More

2K+ Views
We will group Pandas DataFrame using the groupby. Select the column to be used using the grouper function. We will group month-wise and calculate sum of Registration Price monthly for our example shown below for Car Sale Records.At first, let’s say the following is our Pandas DataFrame with three columns −dataFrame = pd.DataFrame( { "Car": ["Audi", "Lexus", "Tesla", "Mercedes", "BMW", "Toyota", "Nissan", "Bentley", "Mustang"], "Date_of_Purchase": [ pd.Timestamp("2021-06-10"), pd.Timestamp("2021-07-11"), pd.Timestamp("2021-06-25"), ... Read More

2K+ Views
To check missing dates, at first, let us set a dictionary of list with date records i.e. Date of Purchase in our example −# dictionary of lists d = {'Car': ['BMW', 'Lexus', 'Audi', 'Mercedes', 'Jaguar', 'Bentley'], 'Date_of_purchase': ['2020-10-10', '2020-10-12', '2020-10-17', '2020-10-16', '2020-10-19', '2020-10-22']}Now, create a dataframe from the above dictionary of lists −dataFrame = pd.DataFrame(d)Next, set it as index −dataFrame = dataFrame.set_index('Date_of_purchase')Use to_datetime() to convert string to DateTime object −dataFrame.index = pd.to_datetime(dataFrame.index) Display remaining dates in a range −k = pd.date_range( start="2020-10-10", end="2020-10-22").difference(dataFrame.index);ExampleFollowing is the code −import pandas as pd # dictionary of lists d = {'Car': ['BMW', ... Read More

11K+ Views
To merge all excel files in a folder, use the Glob module and the append() method.Let’s say the following are our excel files on the Desktop −Sales1.xlsxSales2.xlsxNote − You may need to install openpyxl and xlrd packages.At first, set the path where all the excel files you want to merge are located. Get the excel files and read them using glob −path = "C:\Users\amit_\Desktop\" filenames = glob.glob(path + "\*.xlsx") print('File names:', filenames)Next, create an empty dataframe for the merged output excel file that will get the data from the above two excel files −outputxlsx = pd.DataFrame()Now, the actual process ... Read More

327 Views
Use the Series.value_counts() method to count frequency of itemsets. At first, let us create a DataFrame −# Create DataFrame dataFrame = pd.DataFrame({'Car': ['BMW', 'Mercedes', 'Lamborghini', 'Audi', 'Mercedes', 'Porsche', 'Lamborghini', 'BMW'], 'Place': ['Delhi', 'Hyderabad', 'Chandigarh', 'Bangalore', 'Hyderabad', 'Mumbai', 'Mumbai', 'Pune'], 'UnitsSold': [95, 80, 80, 75, 92, 90, 95, 50 ]})Count the frequency of column car using the value_counts() method −# counting frequency of column Car count1 = dataFrame['Car'].value_counts() print("Count in column Car") print(count1)In the same way, count the frequency of other columns. Following is the complete code to count frequency of itemsets in Pandas DataFrame ... Read More

6K+ Views
The insert() method is used to add a column from another DataFrame. At first, let us create our first DataFrame −dataFrame1 = pd.DataFrame({"Car": ["Audi", "Lamborghini", "BMW", "Lexus"], "Place": ["US", "UK", "India", "Australia"], "Units": [200, 500, 800, 1000]})Now, let us create our second DataFrame −dataFrame2 = pd.DataFrame({"Model": [2018, 2019, 2020, 2021], "CC": [3000, 2800, 3500, 3300]})Car column added from DataFrame1 to DataFrame2# Car column to be added to the second dataframe fetched_col = dataFrame1["Car"]ExampleFollowing is the code −import pandas as pd dataFrame1 = pd.DataFrame({"Car": ["Audi", "Lamborghini", "BMW", "Lexus"], "Place": ["US", "UK", "India", "Australia"], "Units": [200, 500, ... Read More

4K+ Views
When it is required to find the most frequent character in a string, an empty dictionary is created, and the elements in the string are iterated over. When a character is found in the dictionary, it is increment, else it is assigned to 1. The maximum of the values in the dictionary is found, and assigned to a variable.ExampleBelow is a demonstration of the samemy_string = "Python-Interpreter" print ("The string is : ") print(my_string) max_frequency = {} for i in my_string: if i in max_frequency: max_frequency[i] += 1 else: max_frequency[i] ... Read More

355 Views
Multiindex Data Frame is a data frame with more than one index. Let’s say the following is our csv stored on the Desktop −At first, import the pandas library and read the above CSV file −import pandas as pd df = pd.read_csv("C:/Users/amit_/Desktop/sales.csv") print(df)We will form the ‘Car‘ and ‘Place‘ columns of the Dataframe as the index −df = df.set_index(['Car', 'Place'])The DataFrame is now a multi-indexed DataFrame having the ‘Car‘ and ‘Place‘ columns as an index.Now, let us use groupby on the multiindex dataframe:res = df.groupby(level=['Car'])['UnitsSold'].mean() print(res)ExampleFollowing is the code −import pandas as pd df = pd.read_csv("C:/Users/amit_/Desktop/sales.csv") print(df) ... Read More

226 Views
When it is required to find the decreasing point in a list, a simple iteration and the ‘break’ statement are used.ExampleBelow is a demonstration of the same −my_list = [21, 62, 53, 94, 55, 66, 18, 1, 0] print("The list is :") print(my_list) my_result = -1 for index in range(0, len(my_list) - 1): if my_list[index + 1] < my_list[index]: my_result = index break print("The result is :") print(my_result)OutputThe list is : [21, 62, 53, 94, 55, 66, 18, 1, 0] The result is : 1ExplanationA list of integers is ... Read More