Found 26504 Articles for Server Side Programming

Python - How to reset index after Groupby pandas?

AmitDiwan
Updated on 15-Sep-2021 09:31:26

9K+ Views

To reset index after group by, at first group according to a column using groupby(). After that, use reset_index().At first, import the required library −import pandas as pdCreate a DataFrame with 2 columns −dataFrame = pd.DataFrame( { "Car": ["Audi", "Lexus", "Audi", "Mercedes", "Audi", "Lexus", "Mercedes", "Lexus", "Mercedes"], "Reg_Price": [1000, 1400, 1100, 900, 1700, 1800, 1300, 1150, 1350] } ) Group according to Car column −resDF = dataFrame.groupby("Car").mean()Now, reset index after grouping −resDF.reset_index() ExampleFollowing is the code − import pandas as ... Read More

Python - Sum only specific rows of a Pandas Dataframe

AmitDiwan
Updated on 15-Sep-2021 09:16:36

5K+ Views

To sum only specific rows, use the loc() method. Mention the beginning and end row index using the : operator. Using loc(), you can also set the columns to be included. We can display the result in a new column.At first, let us create a DataFrame. We have Product records in it, including the Opening and Closing Stock −dataFrame = pd.DataFrame({"Product": ["SmartTV", "ChromeCast", "Speaker", "Earphone"], "Opening_Stock": [300, 700, 1200, 1500], "Closing_Stock": [200, 500, 1000, 900]})Sum of some rows i.e. 1st two rows. Column names also mentioned in the loc() i.e. Opening_Stock and Closing_Stock. We are displaying result in a new ... Read More

Python - Calculate the median of column values of a Pandas DataFrame

AmitDiwan
Updated on 15-Sep-2021 09:06:09

679 Views

To calculate the median of column values, use the median() method. 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 median of a single column “Units” using median() −print"Median of Units column from DataFrame1 = ", dataFrame1['Units'].median() In the same way, we have calculated the median value from the 2nd DataFrame.ExampleFollowing is the complete code ... Read More

Python Pandas – Find the common rows between two Data Frames

AmitDiwan
Updated on 15-Sep-2021 08:56:53

5K+ Views

To find the common rows between two DataFrames, use the merge() method. Let us first create DataFrame1 with two columns −dataFrame1 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Tesla', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] } )Create DataFrame2 with two columns −dataFrame2 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Units": [100, 250, 150, 80, 130, 90] } )To find the common ... Read More

Python Pandas – Check if any specific column of two DataFrames are equal or not

AmitDiwan
Updated on 15-Sep-2021 08:49:18

372 Views

To check if any specific column of two DataFrames are equal or not, use the equals() method. Let us first create DataFrame1 with two columns −dataFrame1 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] } )Create DataFrame2 with two columns −dataFrame2 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Mercedes', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] ... Read More

Python - Calculate the mean of column values of a Pandas DataFrame

AmitDiwan
Updated on 15-Sep-2021 08:42:35

749 Views

To calculate the mean of column values, use the mean() method. 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 mean of a single column “Units” using mean() −print"Mean of Units column from DataFrame1 = ", dataFrame1['Units'].mean()In the same way, we have calculated the mean value from the 2nd DataFrame.ExampleFollowing is the complete code −import pandas ... Read More

Python - Create a Pipeline in Pandas

AmitDiwan
Updated on 15-Sep-2021 08:16:08

296 Views

To create a pipeline in Pandas, we need to use the pipe() method. At first, import the required pandas library with an alias −import pandas as pdNow, create a DataFrame −dataFrame = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] } ) Create a pipeline and call the upperFunc() custom function to convert column names to uppercase −pipeline = dataFrame.pipe(upperFunc)Following is the upperFun() to convert column names to uppercase −def upperFunc(dataframe): # Converting ... Read More

Python Pandas and Numpy - Concatenate multiindex into single index

AmitDiwan
Updated on 15-Sep-2021 08:06:10

548 Views

To concatenate multiindex into single index, at first, let us import the required Pandas and Numpy libraries with their respective aliases −import pandas as pd import numpy as np Create Pandas series −d = pd.Series([('Jacob', 'North'), ('Ami', 'East'), ('Ami', 'West'), ('Scarlett', 'South'), ('Jacob', 'West'), ('Scarlett', 'North')])Now, use the Numpy arrange() method −dataFrame = pd.Series(np.arange(1, 7), index=d) Let us now map and join −dataMap = dataFrame.index.map('_'.join)ExampleFollowing is the code −import pandas as pd import numpy as np # pandas series d = pd.Series([('Jacob', 'North'), ('Ami', 'East'), ('Ami', 'West'), ('Scarlett', 'South'), ('Jacob', 'West'), ('Scarlett', 'North')]) dataFrame = pd.Series(np.arange(1, 7), ... Read More

Python - Typecasting Pandas into set

AmitDiwan
Updated on 15-Sep-2021 07:58:46

174 Views

To typecast pandas into Set, use the set(). At first, let us create a DataFrame −dataFrame = pd.DataFrame( { "EmpName": ['John', 'Ted', 'Jacob', 'Scarlett', 'Ami', 'Ted', 'Scarlett'], "Zone": ['North', 'South', 'South', 'East', 'West', 'East', 'North'] } ) Typecast pandas to set and then take set union −set(dataFrame.EmpName) | set(dataFrame.Zone)ExampleFollowing is the complete code − import pandas as pd # Create DataFrame dataFrame = pd.DataFrame( { "EmpName": ['John', 'Ted', 'Jacob', 'Scarlett', 'Ami', ... Read More

Python Pandas – Find unique values from multiple columns

AmitDiwan
Updated on 29-Sep-2021 11:07:39

6K+ Views

To find unique values from multiple columns, use the unique() method. Let’s say you have Employee Records with “EmpName” and “Zone” in your Pandas DataFrame. The name and zone can get repeated since two employees can have similar names and a zone can have more than one employee. In that case, if you want unique Employee names, then use the unique() for DataFrame.At first, import the required library. Here, we have set pd as an alias −import pandas as pdAt first, create a DataFrame. Here, we have two columns −dataFrame = pd.DataFrame(    {       "EmpName": ['John', 'Ted', ... Read More

Advertisements