To write a NumPy program that sorts the elements in each column of a given 2D array in ascending order.
- Import NumPy: Start by importing the NumPy library.
- Get Input: Accept a 2D NumPy array from the user.
- Sort Column-wise: Use the
np.sort()function withaxis=0to sort each column in ascending order. - Store Result: Store the sorted result in a new array.
- Display Output: Print the original array and the column-wise sorted array.
import numpy as np
arr=np.array(eval(input()))
print("Given array")
print(arr)
print()
print(np.sort(arr,axis=0))
Thus the program that sorts the elements in each column of a given 2D array in ascending order is executed successfully.
# 2. NumPy Program: Find Indices Where Elements in Array x are Greater Than or Equal to Corresponding Elements in Array y
To write a Python program using NumPy that finds the indices where elements in array x are greater than or equal to their corresponding elements in array y.
- Import NumPy: Import the NumPy library.
- Define Arrays: Define two NumPy arrays,
xandy, with the same shape (i.e., same number of elements). - Use Boolean Indexing:
x > ygives a boolean array where elements ofxare greater thany.x == ygives a boolean array where elements ofxare equal toy.
- Find Indices: Use
np.where()to get the indices where the conditionsx >= yare satisfied. - Print Indices: Print the indices where the condition holds true.
import numpy as np
x=np.array(eval(input()))
y=np.array(eval(input()))
gt=np.where(x>y)
eq=np.where(x==y)
print(gt)
print(eq)Output
Thus the program that finds the indices where elements in array x are greater than or equal to their corresponding elements in array y is executed successfully.
To write a NumPy program that deletes the second column from a given 2D array and inserts a new column at the same position.
- Import NumPy: Start by importing the NumPy library.
- Get Input: Get a 2D NumPy array and a new column (as another array) from the user.
- Delete Column: Use
np.delete()to remove the second column (index 1) from the original array. - Insert Column: Use
np.insert()to insert the new column at the second column's original position. - Display Result: Print the updated array with the replaced column.
import numpy as np
a=np.array(eval(input()))
b=np.array(eval(input()))
print("Printing Original array")
print(a)
print("Array after deleting column 2 on axis 1")
c=np.delete(a,1,axis=1)
print(c)
print("Array after inserting column 2 on axis 1")
print(np.insert(c,1,b,axis=1))
Thus the program that deletes the second column from a given 2D array and inserts a new column at the same position is executed successfully.
To create and display a DataFrame using the Pandas library in Python from a given dictionary, and apply specific index labels to the rows.
- Import Libraries: Import the required libraries β
pandasandnumpy. - Create Dictionary: Define a dictionary
exam_datawith keys:'name','score','attempts', and'qualify'. - Index Labels: Create a list of custom index labels called
labels. - Create DataFrame: Use
pd.DataFrame()to create the DataFrame by passing the dictionary and index labels. - Display Output: Display the DataFrame using
print()or by simply calling the DataFrame variable.
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, 8, 19],
'attempts': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']}
label=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
df = pd.DataFrame(exam_data , index=label)
print(df)
Thus the program to create and display a DataFrame using the Pandas library in Python from a given dictionary, and apply specific index labels to the rows is executed successfully.
To write a Python program using Pandas to join two DataFrames along rows (row-wise concatenation) and assign all data to a new DataFrame.
- Import Libraries: Import the
pandaslibrary. - Create First DataFrame: Use a dictionary to create
student_data1. - Create Second DataFrame: Use another dictionary to create
student_data2. - Concatenate DataFrames: Use
pd.concat()withaxis=0to concatenate both DataFrames row-wise. - Display Result: Print the new combined DataFrame.
import pandas as pd
a=eval(input())
b=eval(input())
df1=pd.DataFrame(a)
df2=pd.DataFrame(b)
print("Original DataFrames:")
print(df1)
print("-------------------------------------")
print(df2)
print()
mer=pd.concat([df1,df2])
print("Join the said two dataframes along rows:")
print(mer)
Thus,The program has been executed successfully




