Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Programming Articles - Page 1064 of 3366
547 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, 100, 150, 200, 130] } )Let us now rename all the columns using columns.values[0[ by setting the index of the column to be changed in the square brackets −dataFrame.columns.values[0] = "Car Names" dataFrame.columns.values[1] = "Registration Cost" dataFrame.columns.values[2] = "Units_Sold"ExampleFollowing is the code −import pandas as pd ... Read More
281 Views
When it is required to print element wise matrix difference, the list elements are iterated over and the zip method is used on these values.ExampleBelow is a demonstration of the samemy_list_1 = [[3, 4, 4], [4, 3, 1], [4, 8, 3]] my_list_2 = [[5, 4, 7], [9, 7, 5], [4, 8, 4]] print("The first list is :") print(my_list_1) print("The second list is :") print(my_list_2) my_result = [] for sub_str_1, sub_str_2 in zip(my_list_1, my_list_2): temp_str = [] for element_1, element_2 in zip(sub_str_1, sub_str_2): temp_str.append(element_2-element_1) my_result.append(temp_str) print("The result is :") print(my_result)OutputThe first ... Read More
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}, {"python": 1, "is": 2, "best": 6}] print("The list is :") print(my_list) my_result = dict() keys = list(my_list[0].keys()) for my_elem in keys: my_result[my_elem] = [min(sub[my_elem] for sub in my_list), max(sub[my_elem] for sub in my_list)] print("The result is :") print(my_result)OutputThe list is : [{'python': 4, ... Read More
420 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 :") print(my_list) my_indices_list = [idx for idx in range( len(my_list)) if my_list[idx] % 2 == 0] my_result = my_indices_list[-1] - my_indices_list[0] print("The result is :") print(my_result)OutputThe list is : [2, 3, 6, 4, 6, 2, 9, 1, 14, 11] The result is : 8ExplanationA list ... Read More
492 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 def ghij"], ["abc2", "gpqr"]] print("The list is :") print(my_list) my_result = [sub for sub in my_list if all(element.isalpha() for element in sub)] print("The result is :") print(my_result)OutputThe list is : [['python', 'is', 'best'], ['abc123', 'good'], ['abc def ghij'], ['abc2', 'gpqr']] The result is : [['python', 'is', 'best']]ExplanationA list ... Read More
300 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"] print("The list is :") print(my_list) index_list = [0, 1, 4] result = 0 for index, element in enumerate(my_list): if index in index_list: result += len(element) print("The result is :") print(result)OutputThe list is : ['python', 'is', 'best', 'for', ... Read More
577 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 : ") print(my_list) range_list = [4, 7, 10, 6] my_result = True for elem in range_list: if elem!= max(my_list): my_result = False break if(elem == True): print("All the elements are ... Read More
812 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 = sum([((x – my_mean) ** 2) for x in my_list]) / len(my_list) my_result = my_variance ** 0.5 print("The result is :") print(result)OutputThe original list : [3, 5, 7, 10, 12] the standard deviation of list is : 3.2619012860600183ExplanationA list is defined and is displayed on the console.The ‘sum’ of the ... Read More
331 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'], "Reg_Price": [5000, 1500, 6500, 8000, 9000, 6000] } )Group using GroupBy and find the Rolling Mean using apply() −dataFrame.groupby("Car")["Reg_Price"].apply( lambda x: x.rolling(center=False, window=2).mean()) ExampleFollowing is the code −import pandas as pd # Create DataFrame dataFrame = pd.DataFrame( { ... Read More
1K+ Views
To delete a column from a DataFrame, use del(). You can also use pop() method to delete. Just drop it using square brackets. Mention the column to be deleted in the brackets and that’s it, for example −del dataFrame[‘ColumnName’]Import the required library with an alias −import pandas as pdCreate a Pandas DataFrame −dataFrame = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] } ) Now, delete a column “Car” from a DataFrame −del ... Read More