Tag Archives: python for beginners

Pandas Example – Write a Pandas program to convert continuous values of a column in a given DataFrame to categorical

(Python Example for Beginners)   Write a Pandas program to convert continuous values of a column in a given DataFrame to categorical. Input: { ‘Name’: [‘Alberto Franco’,’Gino Mcneill’,’Ryan Parkes’, ‘Eesha Hinton’, ‘Syed Wharton’], ‘Age’: [18, 22, 40, 50, 80, 5] } Output: Age group: 0 kids 1 adult 2 elderly 3 adult 4 elderly 5 …

Pandas Example – Write a Pandas program to add a prefix or suffix to all columns of a given DataFrame

(Python Example for Beginners)   Write a Pandas program to add a prefix or suffix to all columns of a given DataFrame.   Sample Solution : Python Code : import pandas as pd df = pd.DataFrame({‘W’:[68,75,86,80,66],’X’:[78,85,96,80,86], ‘Y’:[84,94,89,83,86],’Z’:[86,97,96,72,83]}); print(“Original DataFrame”) print(df) print(“nAdd prefix:”) print(df.add_prefix(“A_”)) print(“nAdd suffix:”) print(df.add_suffix(“_1”)) Sample Output: Original DataFrame W X Y Z 0 68 …

Pandas Example – Write a Pandas program to remove first n rows of a given DataFrame

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

Pandas Example – Write a Pandas program to select all columns, except one given column in a DataFrame

(Python Example for Beginners)   Write a Pandas program to select all columns, except one given column in a 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”) …

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 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 …

Pandas Example – Write a Pandas program to reset index in a given DataFrame

(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 20.0 6 …