AmitDiwan has Published 10744 Articles

How to delete a column from Pandas DataFrame

AmitDiwan

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

Python – Sort Strings by Case difference

AmitDiwan

AmitDiwan

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

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

Python – Filter Similar Case Strings

AmitDiwan

AmitDiwan

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

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

Python – Index Value repetition in List

AmitDiwan

AmitDiwan

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

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

Python – Convert Suffix denomination to Values

AmitDiwan

AmitDiwan

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

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

Python – Check Similar elements in Matrix rows

AmitDiwan

AmitDiwan

Updated on 16-Sep-2021 09:18:43

233 Views

When it is required to check for similar elements in a matrix row, a method is defined that take a matrix as parameter. The map method is used to covert the matrix to a tuple. The matrix values are iterated over and if the frequency is greater than 1, it ... Read More

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

AmitDiwan

AmitDiwan

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

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

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

AmitDiwan

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

Python – Check alternate peak elements in List

AmitDiwan

AmitDiwan

Updated on 16-Sep-2021 09:06:52

188 Views

When it is required to check the alternate peak elements in a list, a function is defined that iterates through the list, the adjacent elements of the array are compared and depending on this, the output is displayed on the console.ExampleBelow is a demonstration of the samedef find_peak(my_array, array_length) : ... Read More

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

AmitDiwan

AmitDiwan

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

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

Advertisements