Hits: 25 (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”) …
Day: April 7, 2021
Hits: 5 (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: …
Hits: 6 (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, …
Hits: 2 (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, …
Hits: 3 (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() …
Hits: 21 (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 …
Hits: 2 (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]} …
Hits: 2 (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 …
Hits: 2 (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 …
Hits: 4 (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 …
Hits: 3 (Python Example for Beginners) Write a Pandas program to reset index in a given DataFrame. Sample data: Original DataFrame attempts name qualify score 0 1 Anastasia yes 12.5 1 3 Dima no 9.0 2 2 Katherine yes 16.5 3 3 James no NaN 4 2 Emily no 9.0 5 3 Michael yes …
Hits: 5 (Python Example for Beginners) Write a Pandas program to drop a list of rows from a specified DataFrame. Sample data: Original DataFrame col1 col2 col3 0 1 4 7 1 4 5 8 2 3 6 9 3 4 7 0 4 5 8 1 New DataFrame after removing 2nd & 4th …