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 1085 of 3363
9K+ Views
To group Pandas data frame, we use groupby(). To sort grouped data frames in ascending or descending order, use sort_values(). The size() method is used to get the data frame size. Steps Involved The steps included in sorting the panda's data frame by its group size are as follows. Importing the panda's library and Creating a Pandas dataframe. Grouping the columns by using the groupby() function and sorting the ... Read More
534 Views
To group columns in Pandas dataframe, use the groupby(). At first, let us create Pandas dataframe −dataFrame = pd.DataFrame( { "Car": ["Audi", "Lexus", "Audi", "Mercedes", "Audi", "Lexus", "Mercedes", "Lexus", "Mercedes"], "Reg_Price": [1000, 1400, 1100, 900, 1700, 1800, 1300, 1150, 1350] } )Let us now group according to Car column −res = dataFrame.groupby("Car")After grouping, we will use functions to find the means Registration prices (Reg_Price) of grouped car names −res.mean()This calculates mean of the Registration price according to column Car.ExampleFollowing is the code −import pandas as pd # dataframe with one of ... Read More
320 Views
To display the index of dataframe in the form of multiindex, use the dataframe.index(). At first, let us create a dictionary of lists −# 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'] }Create a DataFrame from the above dictionary of lists −dataFrame = pd.DataFrame(d)Now, set index column “Car” and display the index −dataFrame.set_index(["Car"], inplace=True, append=True, drop=False) print"Multiindex...", dataFrame.indexExampleFollowing is the code −import pandas as pd # dictionary of lists d = {'Car': ['BMW', 'Lexus', 'Audi', 'Mercedes', 'Jaguar', 'Bentley'], 'Date_of_purchase': ['2020-10-10', ... 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 day-wise and calculate sum of Registration Price with day interval for our example shown below for Car Sale Records.Set the frequency as an interval of days in the groupby() grouper method, that means, if the freq is 7D, that would mean data grouped by interval of 7 days of every month till the last date given in the date column.At first, let’s say the following is our Pandas DataFrame with three columns −import pandas as pd # dataframe ... Read More
3K+ Views
To replace values of a DataFrame with the value of another DataFrame, use the replace() method n Pandas.At first, let us first create a DataFrame −dataFrame1 = pd.DataFrame({"Car": ["Audi", "Lamborghini"], "Place": ["US", "UK"], "Units": [200, 500]})Let us create another DataFrame −dataFrame2 = pd.DataFrame({"Car": ["BMW", "Lexus"], "Place": ["India", "Australia"], "Units": [800, 1000]})Next, get a value from DataFrame2 and replace with a value from DataFrame1 −# get value from 2nd DataFrame i = dataFrame2['Car'][1] # replacing with a value from the 1st DataFrame j = dataFrame1['Car'][0]Finally, use the replace() method to replace the value of one DataFrame with value of another ... Read More
864 Views
We want to replace the negative values with latest preceding positive value. With that, if there’s no positive preceding value, then the value should update to 0.InputFor example, the input is −DataFrame: One two 0 -2 -3 1 4 -7 2 6 5 3 0 -9OutputThe output should be − One two 0 0 0 1 7 0 2 4 2 3 0 2Data Frame masking is used to replace negative values. To fill the missing values, we used forward fill. At first, let ... Read More
909 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