Found 26504 Articles for Server Side Programming

Python - Compute first of group values in a Pandas DataFrame

AmitDiwan
Updated on 29-Sep-2021 07:58:31

171 Views

To compute first of group values, use the groupby.first() method. At first, import the required library with an alias −import pandas as pd;Create a DataFrame with 3 columns −dataFrame = pd.DataFrame(    {       "Car": ['BMW', 'Lexus', 'BMW', 'Tesla', 'Lexus', 'Tesla'], "Place": ['Delhi', 'Bangalore', 'Pune', 'Punjab', 'Chandigarh', 'Mumbai'], "Units": [100, 150, 50, 80, 110, 90] } )Now, group DataFrame by a column −groupDF = dataFrame.groupby("Car") Compute first of group values and resetting index −res = groupDF.first() res = res.reset_index()ExampleFollowing is the complete code − import pandas as pd; dataFrame = pd.DataFrame(    {   ... Read More

How to extract the value names and counts from value_counts() in Pandas?

AmitDiwan
Updated on 29-Sep-2021 07:43:15

400 Views

To extract the value names and counts, let us first create a DataFrame with 4 columns −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] })Fetch the value names and count for a specific column Car −res = dataFrame['Car'].value_counts() Fetch the value names and count for a specific column Units Sold −res = dataFrame['Units Sold'].value_counts()ExampleFollowing is the complete code −import pandas as pd # creating dataframe dataFrame = pd.DataFrame({"Car": ['BMW', ... Read More

Python Pandas – Merge DataFrame with one-to-many relation

AmitDiwan
Updated on 29-Sep-2021 07:35:38

3K+ Views

To merge Pandas DataFrame, use the merge() function. The one-to-many relation is implemented on both the DataFrames by setting under the “validate” parameter of the merge() function i.e. −validate = “one-to-many” or validate = “1:m”The one-to-many relation checks if merge keys are unique in left dataset.At first, let us create our 1st DataFrame −dataFrame1 = pd.DataFrame(    {       "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] } )Now, let us create our 2nd DataFrame −dataFrame2 = pd.DataFrame(    {       "Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', ... Read More

Python Pandas – Merge and create cartesian product from both the DataFrames

AmitDiwan
Updated on 29-Sep-2021 07:21:01

2K+ Views

To merge Pandas DataFrame, use the merge() function. The cartesian product is implemented on both the DataFrames by setting under the “how” parameter of the merge() function i.e. −how = “cross”At first, let us import the pandas library with an alias −import pandas as pd Create DataFrame1 −dataFrame1 = pd.DataFrame(    {       "Car": ['BMW', 'Mustang', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 120] } )Create DataFrame2dataFrame2 = pd.DataFrame(    {       "Car": ['BMW', 'Tesla', 'Jaguar'], "Reg_Price": [7000, 8000, 9000] } )Next, merge DataFrames with "cross" in "how" parameter i.e. ... Read More

Merge Pandas dataframe with a common column and set NaN for unmatched values

AmitDiwan
Updated on 29-Sep-2021 07:09:45

866 Views

To merge two Pandas DataFrame with common column, use the merge() function and set the ON parameter as the column name. To set NaN for unmatched values, use the “how” parameter and set it left or right. That would mean, merging left or right.At first, let us import the pandas library with an alias −import pandas as pdLet us create DataFrame1 −dataFrame1 = pd.DataFrame(    {       "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] } )Let us create DataFrame2dataFrame2 = pd.DataFrame(    {       "Car": ['BMW', ... Read More

Python Pandas - Create Multiindex from dataframe

AmitDiwan
Updated on 29-Sep-2021 06:50:34

688 Views

To create Multiindex from DataFrame, use the MultiIndex. from_frame() method. At first, let us create a 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'] }Next, create a Pandas DataFrame from the above dictionary of lists −dataFrame = pd.DataFrame(d)Now create multiindex using from_frame() −print(pd.MultiIndex.from_frame(dataFrame))ExampleFollowing is the code −import pandas as pd # 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']} # creating dataframe from the above dictionary of lists dataFrame = pd.DataFrame(d) print("DataFrame...", dataFrame) # creating ... Read More

Python Pandas – Check for Null values using notnull()

AmitDiwan
Updated on 28-Sep-2021 12:30:34

6K+ Views

The notnull() method returns a Boolean value i.e. if the DataFrame is having null value(s), then False is returned, else True.Let’s say the following is our CSV file with some NaN i.e. null values −Let us first read the CSV file −dataFrame = pd.read_csv("C:\Users\amit_\Desktop\CarRecords.csv")Checking for not null values −res = dataFrame.notnull()Now, on displaying the DataFrame, the CSV data will be displayed in the form of True and False i.e. boolean values because notnull() returns boolean. For Null values, False will get displayed. For Not-Null values, True will get displayed.ExampleFollowing is the complete code −import pandas as pd # reading ... Read More

Python - How to drop the null rows from a Pandas DataFrame

AmitDiwan
Updated on 28-Sep-2021 12:19:21

2K+ Views

To drop the null rows in a Pandas DataFrame, use the dropna() method. Let’s say the following is our CSV file with some NaN i.e. null values −Let us read the CSV file using read_csv(). Our CSV is on the Desktop −dataFrame = pd.read_csv("C:\Users\amit_\Desktop\CarRecords.csv")Remove the null values using dropna() −dataFrame = dataFrame.dropna() ExampleFollowing is the complete code −import pandas as pd # reading csv file dataFrame = pd.read_csv("C:\Users\amit_\Desktop\CarRecords.csv") print("DataFrame...", dataFrame) # count the rows and columns in a DataFrame print("Number of rows and column in our DataFrame = ", dataFrame.shape) dataFrame = dataFrame.dropna() print("DataFrame after removing null ... Read More

Python Pandas – How to skip initial space from a DataFrame

AmitDiwan
Updated on 28-Sep-2021 12:11:30

6K+ Views

To skip initial space from a Pandas DataFrame, use the skipinitialspace parameter of the read_csv() method. Set the parameter to True to remove extra space.Let’s say the following is our csv file −We should get the following output i.e. skipping initial whitespace and displaying the DataFrame from the CSV −ExampleFollowing is the complete code −import pandas as pd # reading csv file dataFrame = pd.read_csv("C:\Users\amit_\Desktop\CarRecords.csv") print("DataFrame...", dataFrame) # reading csv file and removing initial space dataFrame = pd.read_csv("C:\Users\amit_\Desktop\CarRecords.csv", skipinitialspace = True) print("DataFrame...", dataFrame)At first, read the CSV. Our CSV file is on the Desktop −dataFrame = pd.read_csv("C:\Users\amit_\Desktop\CarRecords.csv")While reading, ... Read More

Python Pandas - Draw a boxplot and display the datapoints on top of boxes by plotting Swarm plot with Seaborn

AmitDiwan
Updated on 28-Sep-2021 11:52:27

460 Views

To plot swarm plot on top of box plot, at first, set boxplot() and then the swarmplot() with the same x and y values. Box Plot in Seaborn is used to draw a box plot to show distributions with respect to categories. The seaborn.boxplot() is used for this.Swarm Plot in Seaborn is used to draw a categorical scatterplot with non-overlapping points. The seaborn.swarmplot() is used for this.Let’s say the following is our dataset in the form of a CSV file − Cricketers.csvAt first, import the required libraries −import seaborn as sb import pandas as pd import matplotlib.pyplot as pltLoad data from ... Read More

Advertisements