Day: April 7, 2021

Pandas Example – Write a Pandas program to convert a given list of lists into a Dataframe

(Python Example for Beginners)   Write a Pandas program to convert a given list of lists into a Dataframe.   Sample Solution : Python Code : import pandas as pd my_lists = [[‘col1’, ‘col2’], [2, 4], [1, 3]] headers = my_lists.pop(0) print(“Original list of lists:”) print(my_lists) df = pd.DataFrame(my_lists, columns = headers) print(“New DataFrame”) print(df) Sample …

Pandas Example – Write a Pandas program to remove infinite values from a given DataFrame

(Python Example for Beginners)   Write a Pandas program to remove infinite values from a given DataFrame.   Sample Solution : Python Code : import pandas as pd import numpy as np df = pd.DataFrame([1000, 2000, 3000, -4000, np.inf, -np.inf]) print(“Original DataFrame:”) print(df) print(“Removing infinite values:”) df = df.replace([np.inf, -np.inf], np.nan) print(df) Sample Output: Original DataFrame: …

Pandas Example – Write a Pandas program to convert the datatype of a given column(float to int)

(Python Example for Beginners)   Write a Pandas program to convert the datatype of a given column(float to int).   Sample Solution : Python Code : import pandas as pd import numpy as np exam_data = {‘name’: [‘Anastasia’, ‘Dima’, ‘Katherine’, ‘James’, ‘Emily’, ‘Michael’, ‘Matthew’, ‘Laura’, ‘Kevin’, ‘Jonas’], ‘score’: [12.5, 9.1, 16.5, 12.77, 9.21, 20.22, 14.5, 11.34, …

Pandas Example – Write a Pandas program to sort a given DataFrame by two or more columns

(Python Example for Beginners)   Write a Pandas program to sort a given DataFrame by two or more columns.   Sample Solution : Python Code : import pandas as pd import numpy as np exam_data = {‘name’: [‘Anastasia’, ‘Dima’, ‘Katherine’, ‘James’, ‘Emily’, ‘Michael’, ‘Matthew’, ‘Laura’, ‘Kevin’, ‘Jonas’], ‘score’: [12.5, 9, 16.5, np.nan, 9, 20, 14.5, np.nan, …

Pandas Example – Write a Pandas program to filter words from a given series that contain atleast two vowels

(Python Example for Beginners)   Write a Pandas program to append data to an empty DataFrame. Sample data: Original DataFrame: After appending some data: col1 col2 0 0 0 1 1 1 2 2 2   Sample Solution : Python Code : import pandas as pd import numpy as np df = pd.DataFrame() data = …

Pandas Example – Write a Pandas program to get the specified row value of a given DataFrame

(Python Example for Beginners)   Write a Pandas program to get the specified row value of a given DataFrame.   Sample Solution : Python Code : import pandas as pd d = {‘col1’: [1, 2, 3, 4, 7], ‘col2’: [4, 5, 6, 9, 5], ‘col3’: [7, 8, 12, 1, 11]} df = pd.DataFrame(data=d) print(“Original DataFrame”) print(df) …

Pandas Example – Write a Pandas program to find the row for where the value of a given column is maximum

(Python Example for Beginners)   Write a Pandas program to find the row for where the value of a given column is maximum.   Sample Solution: Python Code : import pandas as pd d = {‘col1’: [1, 2, 3, 4, 7], ‘col2’: [4, 5, 6, 9, 5], ‘col3’: [7, 8, 12, 1, 11]} df = …

Pandas Example – Write a Pandas program to get a list of a specified column of a DataFrame

(Python Example for Beginners)   Write a Pandas program to get a list of a specified column of a DataFrame. Sample data: Powered by Original DataFrame col1 col2 col3 0 1 4 7 1 2 5 8 2 3 6 9 Col2 of the DataFrame to list: [4, 5, 6]   Sample Solution : Python …

Pandas Example – Write a Pandas program to convert DataFrame column type from string to datetime

(Python Example for Beginners)   Write a Pandas program to convert DataFrame column type from string to datetime. Sample data: String Date: 0 3/11/2000 1 3/12/2000 2 3/13/2000 dtype: object Original DataFrame (string to datetime): 0 0 2000-03-11 1 2000-03-12 2 2000-03-13   Sample Solution : Python Code : import pandas as pd import numpy as …

Pandas Example – Write a Pandas program to combining two series into a DataFrame

(Python Example for Beginners)   Write a Pandas program to combining two series into a DataFrame. Sample data: Data Series: 0 100 1 200 2 python 3 300.12 4 400 dtype: object 0 10 1 20 2 php 3 30.12 4 40 dtype: object New DataFrame combining two series: 0 1 0 100 10 1 …