Found 26504 Articles for Server Side Programming

Python Pandas – Check if two Dataframes are exactly same

AmitDiwan
Updated on 14-Sep-2021 12:12:17

700 Views

The equals() function is used to check if two dataframes are exactly same. At first, let us 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'], "Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000] ... Read More

Count unique values per groups in Python Pandas

Rishikesh Kumar Rishi
Updated on 14-Sep-2021 12:07:37

6K+ Views

To count unique values per groups in Python Pandas, we can use df.groupby('column_name').count().StepsCreate a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.Print the input DataFrame, df.Use df.groupby('rank')['id'].count() to find the count of unique values per groups and store it in a variable "count".Print the count from Step 3.Exampleimport pandas as pd df = pd.DataFrame(     {        "id": [1, 2, 1, 3, 5, 1, 4, 3, 6, 7],        'rank': [1, 4, 1, 2, 1, 4, 6, 1, 5, 3]     } ) print"Input DataFrame 1 is:", df count = df.groupby('rank')['id'].count() print"Frequency of ranks:", countOutputInput DataFrame 1 is:    id  rank 0   1    1 1   2    4 2   1    1 3   3    2 4   5    1 5   1    4 6   4    6 7   3    1 8   6    5 9   7    3 Frequency of ranks: rank 1  4 2  1 3  1 4  2 5  1 6  1 Name: id, dtype: int64

Python Pandas – Find the Difference between two Dataframes

AmitDiwan
Updated on 14-Sep-2021 11:57:22

792 Views

To find the difference between two DataFrame, you need to check for its equality. Also, check the equality of columns.Let us 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', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] } ... Read More

Group-by and Sum in Python Pandas

Rishikesh Kumar Rishi
Updated on 14-Sep-2021 11:59:23

6K+ Views

To find group-by and sum in Python Pandas, we can use groupby(columns).sum().StepsCreate a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.Print the input DataFrame, df.Find the groupby sum using df.groupby().sum(). This function takes a given column and sorts its values. After that, based on the sorted values, it also sorts the values of other columns.Print the groupby sum.Exampleimport pandas as pd df = pd.DataFrame(     {        "Apple": [5, 2, 7, 0],        "Banana": [4, 7, 5, 1],        "Carrot": [9, 3, 5, 1]     } ) print "Input DataFrame 1 ... Read More

Python – Create a Subset of columns using filter()

AmitDiwan
Updated on 14-Sep-2021 11:55:22

292 Views

To create a subset of columns, we can use filter(). Through this, we can filter column values with similar pattern using like operator. At first, let us create a DataFrame with 3 columns −dataFrame = pd.DataFrame({"Product": ["SmartTV", "ChromeCast", "Speaker", "Earphone"], "Opening_Stock": [300, 700, 1200, 1500], "Closing_Stock": [200, 500, 1000, 900]})Now, let us create a subset with multiple columns −dataFrame[['Opening_Stock', 'Closing_Stock']]Create a subset with similarly patterned names −dataFrame.filter(like='Open')ExampleFollowing is the complete code −import pandas as pd dataFrame = pd.DataFrame({"Product": ["SmartTV", "ChromeCast", "Speaker", "Earphone"], "Opening_Stock": [300, 700, 1200, 1500], "Closing_Stock": [200, 500, 1000, 900]}) print"DataFrame...", dataFrame print"Displaying a subset ... Read More

How to get column index from column name in Python Pandas?

Rishikesh Kumar Rishi
Updated on 14-Sep-2021 11:45:05

12K+ Views

To get column index from column name in Python Pandas, we can use the get_loc() method.Steps −Create a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.Print the input DataFrame, df.Find the columns of DataFrame, using df.columns.Print the columns from Step 3.Initialize a variable column_name.Get the location, i.e., of index for column_name.Print the index of the column_name.Example −import pandas as pd df = pd.DataFrame(    {       "x": [5, 2, 7, 0],       "y": [4, 7, 5, 1],       "z": [9, 3, 5, 1]    } ) print"Input DataFrame 1 is:", df columns = ... Read More

Python program to construct Equidigit tuples

AmitDiwan
Updated on 14-Sep-2021 11:23:36

116 Views

When it is required to construct equi-digit tuples, the ‘//’ operator and the list slicing is used.ExampleBelow is a demonstration of the samemy_list = [5613, 1223, 966143, 890, 65, 10221] print("The list is :") print(my_list) my_result = [] for sub in my_list: mid_index = len(str(sub)) // 2 element_1 = str(sub)[:mid_index] element_2 = str(sub)[mid_index:] my_result.append((int(element_1), int(element_2))) print("The resultant list is :") print(my_result)OutputThe list is : [5613, 1223, 966143, 890, 65, 10221] The resultant list is : [(56, 13), (12, 23), (966, 143), (8, 90), ... Read More

Python program to omit K length Rows

AmitDiwan
Updated on 14-Sep-2021 11:21:15

195 Views

When it is required to omit K length rows, a simple iteration and the ‘len’ method along with ‘append’ method are used.ExampleBelow is a demonstration of the samemy_list = [[41, 7], [8, 10, 12, 8], [10, 11], [6, 82, 10]] print("The list is :") print(my_list) my_k = 2 print("The value of K is ") print(my_k) my_result = [] for row in my_list: if len(row) != my_k : my_result.append(row) print("The resultant list is :") print(my_result)OutputThe list is : [[41, 7], [8, 10, 12, 8], [10, 11], [6, ... Read More

Python program to extract rows with common difference elements

AmitDiwan
Updated on 14-Sep-2021 11:19:36

112 Views

When it is required to extract rows with common difference elements, an iteration and a flag value is used.ExampleBelow is a demonstration of the samemy_list = [[31, 27, 10], [8, 11, 12], [11, 12, 13], [6, 9, 10]] print("The list is :") print(my_list) my_result = [] for row in my_list:    temp = True    for index in range(0, len(row) - 1): if row[index + 1] - row[index] != row[1] - row[0]: temp = False ... Read More

Python program to compute a Polynomial Equation

AmitDiwan
Updated on 14-Sep-2021 11:13:43

320 Views

When it is required to compute a polynomial equation, a simple iteration along with the ‘*’ operator is used.ExampleBelow is a demonstration of the samemy_list = [3, -6, 3, -1, 23, -11, 0, -8] print("The list is :") print(my_list) x = 3 my_list_length = len(my_list) my_result = 0 for i in range(my_list_length): my_sum = my_list[i] for j in range(my_list_length - i - 1): my_sum = my_sum * x my_result = my_result + my_sum print("The result is :") print(my_result)OutputThe list ... Read More

Advertisements