[go: up one dir, main page]

0% found this document useful (0 votes)
9 views3 pages

Numpy Exercise

Uploaded by

johibel889
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

Numpy Exercise

Uploaded by

johibel889
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Numpy Exercise

https://www.machinelearningplus.com/python/101-numpy-exercises-python/
1. Create a 1D array of numbers from 0 to 9
2. Create a 3×3 numpy array of all True’s
3. Extract all odd numbers from arr Input: arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8,
9])
4. Replace all odd numbers in arr with -1 arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
5. Convert a 1D array to a 2D array with 2 rows
6. Stack arrays a and b vertically
7. Stack the arrays a and b horizontally.
8. Create the following pattern:

9. Get the common items between a and b


a = np.array([1,2,3,2,3,4,3,4,5,6])
b = np.array([7,2,10,2,7,4,9,4,9,8])

10. From array a remove all items present in array b

11. Get the positions where elements of a and b match

12. Get all items between 5 and 10 from a


13. Swap columns 1 and 2 in the array arr.
14. Swap rows 1 and 2 in the array arr.

15. Create a 2D array of shape 5x3 to contain random decimal numbers between 5
and 10.
16. Print or show only 3 decimal places of the numpy array rand_arr.
Input: rand_arr = np.random.random((5,3))

17. Limit the number of items printed in python numpy array a to a maximum of 6
elements.

https://libguides.ntu.edu.sg/python/numpyexercises
18. Replace all odd numbers in the given array with -1

19. Add 202 to all the values in given array

20. Generate a 1-D array of 10 random integers. Each integer should be a number
between 30 and 40 (inclusive)
Sample of desired output: [36, 30, 36, 38, 31, 35, 36, 30, 32, 34]
21. Find the positions of:
i. elements in x where its value is more than its corresponding element in y, and
ii. elements in x where its value is equals to its corresponding element in y.
Start with these:
x = np.array([21, 64, 86, 22, 74, 55, 81, 79, 90, 89])
y = np.array([21, 7, 3, 45, 10, 29, 55, 4, 37, 18])
Desired output: (array([1, 2, 4, 5, 6, 7, 8, 9]),) and (array([0]),)
22. Extract the first four columns of this 2-D array
Start with this: exercise_6 = np.arange(100).reshape(5,-1)
pandas Exercise
https://www.machinelearningplus.com/python/101-pandas-exercises-python/
1. Create a pandas series from each of the items below: a list, numpy and a
dictionary
Input
mylist = list('abcedfghijklmnopqrstuvwxyz')
myarr = np.arange(26)
mydict = dict(zip(mylist, myarr))
2. Convert the series ser into a dataframe with its index as another column on
the dataframe.
Input
mylist = list('abcedfghijklmnopqrstuvwxyz')
myarr = np.arange(26)
mydict = dict(zip(mylist, myarr))
ser = pd.Series(mydict)
3. Combine ser1 and ser2 to form a dataframe.
Input ser1 = pd.Series(list('abcedfghijklmnopqrstuvwxyz'))
ser2 = pd.Series(np.arange(26))
4. Reshape the series ser into a dataframe with 7 rows and 5 columns
Input ser = pd.Series(np.random.randint(1, 10, 35))
https://www.w3resource.com/python-exercises/pandas/index-dataframe.php
5. Write a Pandas program to create a dataframe from a dictionary and display it.
Sample data: {'X':[78,85,96,80,86],
6. 'Y':[84,94,89,83,86],'Z':[86,97,96,72,83]}
7. Write a Pandas program to get the first 3 rows of a given DataFrame.
8. Write a Pandas program to count the number of rows and columns of a DataFrame.
9. Write a Pandas program to select the rows the Y is between 83 and 89 (inclusive).
10. Write a Pandas program to calculate the sum of the examination attempts by
the students. Sample Python dictionary data and list labels:
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']}
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
11. Write a Pandas program to calculate the mean of all students' scores. Data is
stored in a dataframe.
12. Write a Pandas program to sort the DataFrame first by 'name' in descending
order, then by 'score' in ascending order.
13. Write a Pandas program to write a DataFrame to CSV file using tab
separator.
14. Write a Pandas program to count city wise number of people from a given of
data set (city, name of the person).
Sample data:
city Number of people
0 California 4
1 Georgia 2
2 Los Angeles 4
15. Write a Pandas program to convert index in a column of the given dataframe.

You might also like