Found 26504 Articles for Server Side Programming

Python – Mean deviation of Elements

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

795 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

Find Rolling Mean – Python Pandas

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

313 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

How to delete a column from Pandas DataFrame

AmitDiwan
Updated on 16-Sep-2021 09:31:48

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

Python – Sort Strings by Case difference

AmitDiwan
Updated on 16-Sep-2021 09:26:40

185 Views

When it is required to sort strings based on case difference, a method is defined that takes a string as a parameter. This method uses list comprehension and ‘isupper’ and ‘islower’ methods along with list comprehension to get case difference. Their difference gives the sorted values.ExampleBelow is a demonstration of the samedef get_diff(my_string):    lower_count = len([ele for ele in my_string if ele.islower()])    upper_count = len([ele for ele in my_string if ele.isupper()])    return abs(lower_count - upper_count) my_list = ["Abc", "Python", "best", "hello", "coders"] print("The list is :") print(my_list) my_list.sort(key=get_diff) print("Sorted Strings by case ... Read More

Python – Filter Similar Case Strings

AmitDiwan
Updated on 16-Sep-2021 09:23:53

223 Views

When it is required to filter similar case strings, list comprehension can be used along with ‘isupper’ and ‘islower’ methods.ExampleBelow is a demonstration of the samemy_list = ["Python", "good", "FOr", "few", "CODERS"] print("The list is :") print(my_list) my_result = [sub for sub in my_list if sub.islower() or sub.isupper()] print("The strings with same case are :") print(my_result)OutputThe list is : ['Python', 'good', 'FOr', 'few', 'CODERS'] The strings with same case are : ['good', 'few', 'CODERS']ExplanationA list is defined and is displayed on the console.The list comprehension is used to iterate over the list and check if the strings ... Read More

Python – Index Value repetition in List

AmitDiwan
Updated on 16-Sep-2021 09:22:21

273 Views

When it is required to find the index value that has been repeated in a list, it is iterated over using the list comprehension and ‘enumerate’.ExampleBelow is a demonstration of the samemy_list = [4, 0, 3, 1] print("The list is :") print(my_list) my_result = [element for sub in ([index] * element for index, element in enumerate(my_list)) for element in sub] print("The result is :") print(my_result)OutputThe list is : [4, 0, 3, 1] The result is : [0, 0, 0, 0, 2, 2, 2, 3]ExplanationA list is defined and is displayed on the console.List comprehension is used to ... Read More

Python Pandas - How to select rows from a DataFrame by integer location

AmitDiwan
Updated on 16-Sep-2021 09:16:53

870 Views

To select rows by integer location, use the iloc() function. Mention the index number of the row you want to select.Create a DataFrame −dataFrame = pd.DataFrame([[10, 15], [20, 25], [30, 35]], index=['x', 'y', 'z'], columns=['a', 'b'])Select rows with integer location using iloc() −dataFrame.iloc[1] ExampleFollowing is the code − import pandas as pd # Create DataFrame dataFrame = pd.DataFrame([[10, 15], [20, 25], [30, 35]], index=['x', 'y', 'z'], columns=['a', 'b']) # DataFrame print"DataFrame...", dataFrame # select rows with loc print"Select rows by passing label..." print(dataFrame.loc['z']) # select rows with integer location using iloc print"Select rows by passing integer ... Read More

Python – Convert Suffix denomination to Values

AmitDiwan
Updated on 16-Sep-2021 09:21:11

204 Views

When it is required to convert the suffix denomination to values, the dictionary is iterated over and the ‘replace’ method is used to convert them to values.ExampleBelow is a demonstration of the samemy_list = ["5Cr", "7M", "9B", "12L", "20Tr", "30K"] print("The list is :") print(my_list) value_dict = {"M": 1000000, "B": 1000000000, "Cr": 10000000, "L": 100000, "K": 1000, "Tr": 1000000000000} my_result = [] for element in my_list: for key in value_dict: if key in element: val = ... Read More

Python Pandas - How to select rows from a DataFrame by passing row label

AmitDiwan
Updated on 16-Sep-2021 09:12:58

4K+ Views

To select rows by passing a label, use the loc() function. Mention the index of which you want to select the row. This is the index label in our example. We have x, y and z as the index label and can be used to select rows with loc().Create a DataFrame −dataFrame = pd.DataFrame([[10, 15], [20, 25], [30, 35]], index=['x', 'y', 'z'], columns=['a', 'b'])Now, select rows with loc. We have passed the index label “z” −dataFrame.loc['z'] ExampleFollowing is the code −import pandas as pd # Create DataFrame dataFrame = pd.DataFrame([[10, 15], [20, 25], [30, 35]], index=['x', 'y', 'z'], columns=['a', ... Read More

Python - Cast datatype of only a single column in a Pandas DataFrame

AmitDiwan
Updated on 16-Sep-2021 09:01:31

507 Views

To cast only a single column, use the astype() method. Let us first create a DataFrame with 2 columns. One of them is a “float64” type and another “int64” −dataFrame = pd.DataFrame( { "Reg_Price": [7000.5057, 1500, 5000, 8000, 9000.75768, 6000], "Units": [90, 120, 100, 150, 200, 130] } )Check the types −dataFrame.dtypes Let’s say we need to cast only a single column “Units” from int64 to int32. For that, use astype() −dataFrame.astype({'Units': 'int32'}).dtypesExampleFollowing is the code − import pandas as pd ... Read More

Advertisements