AmitDiwan has Published 10744 Articles

Python - Convert Pandas DataFrame to binary data

AmitDiwan

AmitDiwan

Updated on 20-Sep-2021 07:27:52

4K+ Views

Use the get_dummies() method to convert categorical DataFrame to binary data. Following is our Pandas DataFrame with 2 columns −dataFrame = pd.DataFrame(    {       "Student": ['Jack', 'Robin', 'Ted', 'Scarlett', 'Kat'], "Result": ['Pass', 'Fail', 'Fail', 'Pass', 'Pass'] } )Use the get_dummies() and set the column ... Read More

Renaming column names – Python Pandas

AmitDiwan

AmitDiwan

Updated on 20-Sep-2021 07:21:18

281 Views

We can use the rename() method to rename column names. Let’s say the following is our Pandas DataFrame with 3 columns −dataFrame = pd.DataFrame(    {       "Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], "Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000], "Units": [90, 120, 100, 150, 200, 130] ... Read More

Python – Limit the values to keys in a Dictionary List

AmitDiwan

AmitDiwan

Updated on 20-Sep-2021 07:14:43

1K+ Views

When it is required to limit the values to keys in a list of dictionary, the keys are accessed and the ‘min’ and ‘max’ methods are used to limit the values.ExampleBelow is a demonstration of the samemy_list = [{"python": 4, "is": 7, "best": 10}, {"python": 2, "is": 5, "best": 9}, ... Read More

Python - Rename column names by index in a Pandas DataFrame without using rename()

AmitDiwan

AmitDiwan

Updated on 20-Sep-2021 07:13:51

534 Views

We can easily rename a column by index i.e. without using rename(). Import the required library −import pandas as pdCreate a DataFrame with 3 columns −dataFrame = pd.DataFrame(    {       "Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], "Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000], "Units": [90, 120, ... Read More

Python – Find the distance between first and last even elements in a List

AmitDiwan

AmitDiwan

Updated on 20-Sep-2021 06:48:21

410 Views

When it is required to find the distance between the first and last even elements of a list, list elements are accessed using indexing and the difference is found.ExampleBelow is a demonstration of the samemy_list = [2, 3, 6, 4, 6, 2, 9, 1, 14, 11] print("The list is ... Read More

Python – Filter rows with only Alphabets from List of Lists

AmitDiwan

AmitDiwan

Updated on 20-Sep-2021 06:45:59

482 Views

When it is required to filter the rows that contains only alphabets in a list of lists, the list is iterated over and the ‘isalpha’ method is used to check if an alphabet is present or not.ExampleBelow is a demonstration of the samemy_list = [["python", "is", "best"], ["abc123", "good"], ["abc ... Read More

Python – Find the sum of Length of Strings at given indices

AmitDiwan

AmitDiwan

Updated on 20-Sep-2021 06:43:55

284 Views

When it is required to find the sum of the length of string at specific indices, the ‘enumerate’ is used to iterate through the elements in the list and adding the length of the element to a list.ExampleBelow is a demonstration of the samemy_list = ["python", "is", "best", "for", "coders"] ... Read More

Python – Test if elements of list are in Min/Max range from other list

AmitDiwan

AmitDiwan

Updated on 20-Sep-2021 06:42:12

565 Views

When it is required to test if the elements are in the min/max range, the list elements are iterated over, and are checked to see if it is equal to ‘max’ value.ExampleBelow is a demonstration of the samemy_list = [5, 6, 4, 7, 8, 13, 15] print("The list is : ... Read More

Python – Mean deviation of Elements

AmitDiwan

AmitDiwan

Updated on 20-Sep-2021 06:37:24

791 Views

When it is required to find the mean deviation of the elements of a list, the ‘sum’ method and the ‘len’ method is used.ExampleBelow is a demonstration of the samemy_list = [3, 5, 7, 10, 12] print("The list is :") print(my_list) my_mean = sum(my_list) / len(my_list) my_variance = ... Read More

Find Rolling Mean – Python Pandas

AmitDiwan

AmitDiwan

Updated on 17-Sep-2021 07:30:12

310 Views

To find rolling mean, we will use the apply() function in Pandas. At first, let us import the required library −import pandas as pdCreate a DataFrame with 2 columns. One is an int column −dataFrame = pd.DataFrame(    {       "Car": ['Tesla', 'Mercedes', 'Tesla', 'Mustang', 'Mercedes', 'Mustang'],   ... Read More

Advertisements