Found 10476 Articles for Python

How to append a list as a row to a Pandas DataFrame in Python?

AmitDiwan
Updated on 21-Sep-2021 07:18:44

2K+ Views

To open a list, we can use append() method. With that, we can also use loc() method. At first, let us import the required library −import pandas as pdFollowing is the data in the form of lists of team rankings −Team = [['India', 1, 100], ['Australia', 2, 85], ['England', 3, 75], ['New Zealand', 4 , 65], ['South Africa', 5, 50]]Creating a DataFrame with the above data and adding columns −dataFrame = pd.DataFrame(Team, columns=['Country', 'Rank', 'Points']) Let’s say the following is the row to be appended −myList = [["Sri Lanka", 6, 40]]Append the above row in the form of list − ... Read More

Python program to remove words that are common in two Strings

AmitDiwan
Updated on 21-Sep-2021 07:10:18

912 Views

When it is required to remove words that are common in both the strings, a method is defined that takes two strings. The strings are spit based on spaces and list comprehension is used to filter out the result.ExampleBelow is a demonstration of the samedef common_words_filter(my_string_1, my_string_2):    my_word_count = {}    for word in my_string_1.split(): my_word_count[word] = my_word_count.get(word, 0) + 1 for word in my_string_2.split(): my_word_count[word] = my_word_count.get(word, 0) + 1 return [word for ... Read More

Python Program to Extract Elements from a List in a Set

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

388 Views

When it is required to extract elements from a List in a Set, a simple ‘for’ loop and a base condition can be used.ExampleBelow is a demonstration of the samemy_list = [5, 7, 2, 7, 2, 4, 9, 8, 8] print("The list is :") print(my_list) search_set = {6, 2, 8} my_result = [] for element in my_list: if element in search_set: my_result.append(element) print("The result is :") print(my_result)OutputThe list is : [5, 7, 2, 7, 2, 4, 9, 8, 8] The result is : [2, 2, 8, ... Read More

Python - Concatenate Pandas DataFrames Without Duplicates

AmitDiwan
Updated on 21-Sep-2021 07:09:41

9K+ Views

To concatenate DataFrames, use the concat() method, but to ignore duplicates, use the drop_duplicates() method.Import the required library −import pandas as pdCreate DataFrames to be concatenated −# Create DataFrame1 dataFrame1 = pd.DataFrame(    {       "Car": ['BMW', 'Jaguar', 'Audi', 'Mustang'], "Units": [100, 150, 110, 80]    } ) # Create DataFrame2 dataFrame2 = pd.DataFrame(    {       "Car": ['Tesla', 'Jaguar', 'Mercedes', 'Mustang'], "Units": [120, 150, 180, 80] } ) Now, let us concatenate DataFrame and remove duplicates −concatRes = pd.concat([dataFrame1, dataFrame2]).drop_duplicates()ExampleFollowing is the code −import pandas as pd # ... Read More

Python Pandas - Filling missing column values with median

AmitDiwan
Updated on 21-Sep-2021 07:03:26

3K+ Views

Median separates the higher half from the lower half of the data. Use the fillna() method and set the median to fill missing columns with median. At first, let us import the required libraries with their respective aliases −import pandas as pd import numpy as npCreate a DataFrame with 2 columns. We have set the NaN values using the Numpy np.NaN −dataFrame = pd.DataFrame(    {       "Car": ['Lexus', 'BMW', 'Audi', 'Bentley', 'Mustang', 'Tesla'], "Units": [100, 150, np.NaN, 80, np.NaN, np.NaN] } )Find median of the column values with NaN i.e, for Units columns here. ... Read More

Python program to mask a list using values from another list

AmitDiwan
Updated on 21-Sep-2021 07:04:15

757 Views

When it is required to mask a list with the help of values from another list, list comprehension is used.ExampleBelow is a demonstration of the samemy_list = [5, 6, 1, 9, 11, 0, 4] print("The list is :") print(my_list) search_list = [2, 10, 6, 3, 9] result = [1 if element in search_list else 0 for element in my_list] print("The result is :") print(result)OutputThe list is : [5, 6, 1, 9, 11, 0, 4] The result is : [0, 1, 0, 1, 0, 0, 0]ExplanationA list is defined and is displayed on the console.Another list of ... Read More

How to append a list to a Pandas DataFrame using loc in Python?

AmitDiwan
Updated on 21-Sep-2021 06:58:04

797 Views

The Dataframe.loc is used to access a group of rows and columns by label or a boolean array. We will append a list to a DataFrame using loc. Let us first create a DataFrame. The data is in the form of lists of team rankings for our example −# data in the form of list of team rankings Team = [['India', 1, 100], ['Australia', 2, 85], ['England', 3, 75], ['New Zealand', 4 , 65], ['South Africa', 5, 50], ['Bangladesh', 6, 40]] # Creating a DataFrame and adding columns dataFrame = pd.DataFrame(Team, columns=['Country', 'Rank', 'Points'])Following is the row to be ... Read More

Python Pandas - Filling missing column values with mode

AmitDiwan
Updated on 21-Sep-2021 06:51:07

2K+ Views

Mode is the value that appears the most in a set of values. Use the fillna() method and set the mode to fill missing columns with mode. At first, let us import the required libraries with their respective aliases −import pandas as pd import numpy as npCreate a DataFrame with 2 columns. We have set the NaN values using the Numpy np.NaN −dataFrame = pd.DataFrame(    {       "Car": ['BMW', 'Lexus', 'Lexus', 'Mustang', 'Bentley', 'Mustang'], "Units": [100, 150, np.NaN, 80, np.NaN, np.NaN] } )Find mode of the column values with NaN i.e, for Units columns ... Read More

Python - Search DataFrame for a specific value with pandas

AmitDiwan
Updated on 21-Sep-2021 06:32:44

2K+ Views

We can search DataFrame for a specific value. Use iloc to fetch the required value and display the entire row. At first, import the required library −import pandas as pdCreate a DataFrame with 4 columns −dataFrame = pd.DataFrame({"Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], "Cubic_Capacity": [2000, 1800, 1500, 2500, 2200, 3000], "Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000], "Units_Sold": [ 100, 120, 150, 110, 200, 250] })Let’s search Car with Registeration Price 500 −for i in range(len(dataFrame.Car)): if 5000 == dataFrame.Reg_Price[i]: indx = iNow, display the found value −dataFrame.iloc[indx] ExampleFollowing is ... Read More

Sort index in ascending order – Python Pandas

AmitDiwan
Updated on 21-Sep-2021 06:23:07

825 Views

The sort_index() is used to sort index in ascending and descending order. If you won’t mention any parameter, then index sorts in ascending order.At first, import the required library −import pandas as pdCreate a new DataFrame. It has unsorted indexes −dataFrame = pd.DataFrame([100, 150, 200, 250, 250, 500],index=[4, 8, 2, 9, 15, 11],columns=['Col1'])Sort the indexes −dataFrame.sort_index() ExampleFollowing is the code −import pandas as pd dataFrame = pd.DataFrame([100, 150, 200, 250, 250, 500],index=[4, 8, 2, 9, 15, 11],columns=['Col1']) print"DataFrame...",dataFrame print"Sort index...",dataFrame.sort_index()OutputThis will produce the following output −DataFrame...    Col1 4 100 8 150 2 200 9 250 15 250 11 500 Sort index... Col1 2 200 4 100 8 150 9 250 11 500 15 250

Advertisements