AmitDiwan has Published 10744 Articles

Python – 3D Matrix to Coordinate List

AmitDiwan

AmitDiwan

Updated on 13-Sep-2021 07:22:19

423 Views

When it is required to convert a three dimensional matrix into a co-ordinate list, the ‘zip’ method, and a list comprehension are used.ExampleBelow is a demonstration of the same −my_list_1 = [[['He', 'Wi'], ['llo', 'll']], [['Pyt', 'i'], ['hon', 'sFun']], [['Ho', 'g'], ['pe', 'ood']]] print("The list is : ") print(my_list_1) ... Read More

Python – Cross Pairing in Tuple List

AmitDiwan

AmitDiwan

Updated on 13-Sep-2021 07:19:29

295 Views

When it is required to perform cross pairing in a list of tuples, the ‘zip’ method, a list comprehension and the ‘==’ operator is used.ExampleBelow is a demonstration of the same −my_list_1 = [('Hi', 'Will'), ('Jack', 'Python'), ('Bill', 'Mills'), ('goodwill', 'Jill')] my_list_2 = [('Hi', 'Band'), ('Jack', 'width'), ('Bill', 'cool'), ('a', ... Read More

Python - How to Group Pandas DataFrame by Days?

AmitDiwan

AmitDiwan

Updated on 09-Sep-2021 14:44:45

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() ... Read More

Python - Grouping columns in Pandas Dataframe

AmitDiwan

AmitDiwan

Updated on 09-Sep-2021 14:23:47

502 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 ... Read More

Python Pandas - Display the index of dataframe in the form of multi-index

AmitDiwan

AmitDiwan

Updated on 09-Sep-2021 14:14:30

280 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 ... Read More

Python - Replace values of a DataFrame with the value of another DataFrame in Pandas

AmitDiwan

AmitDiwan

Updated on 09-Sep-2021 13:32:53

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, ... Read More

Python - Replace negative values with latest preceding positive value in Pandas DataFrame

AmitDiwan

AmitDiwan

Updated on 09-Sep-2021 13:21:37

825 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 ... Read More

Python - Drop specific rows from multiindex Pandas Dataframe

AmitDiwan

AmitDiwan

Updated on 09-Sep-2021 12:36:42

870 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( ... Read More

Python - Sum negative and positive values using GroupBy in Pandas

AmitDiwan

AmitDiwan

Updated on 09-Sep-2021 11:55:40

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 ... Read More

Python - How to Group Pandas DataFrame by Month?

AmitDiwan

AmitDiwan

Updated on 09-Sep-2021 11:41:00

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 ... Read More

Advertisements