Numpy and Pandas
Numpy
Numerical Python
Fundamental package for high performance scientific computing and data analysis
Provides multi-dimensional array objects with vector operation capabilities
Pandas
Based on Numpy, it provides high-performance matrix operations
Has more convenient methods used in matrix calculations
one-dimensional array
Pandas:Series
Numpy:ndarray
Similarities between ndarray and Python List
Access elements by position
l[0]、l[2:5]、l[:3]、l[2:]
cycle:
for item in l:
xxx
Difference between ndarray and Python List
1. Python List elements can be combined with any type, and ndarray element types must be the same (Numpy will automatically do type conversion when they are not the same)
2. ndaarray has mean(), std() and more built-in functions related to mathematical calculations
3.ndarray can be more convenient to operate on multi-dimensional arrays
import numpy as np
import pandas as pd
print(type(np.array([1, 2, 3])))
print(type(pd.Series([1, 2, 3])))
hours = np.array(['12AM', '1AM', '2AM', '3AM',
'4AM', '5AM', '6AM', '7AM',
'8AM', '9AM', '10AM', '11AM',
'1PM', '2PM', '3PM', '4PM',
'5PM', '6PM', '7PM', '8PM',
'9PM', '10PM', '11PM', '12PM'])
Temps = np.array([-1, -1, -1, -1,
-1, -1, -1, -1,
-1, -1, -1, -1,
-1, -1, -1, -1,
-1, -1, -1, -1,
-1, -1, -1, -1,
])
print(hours[14])
print(hours[2:4])
print(Temps[2:4])
for i in hours:
print(i, end=" ")
print()
print(Temps.dtype)
print(np.array([1,2,3]).dtype)
print(np.array(['HELLO',1,True]).dtype)
for i in np.array(['Hello',1,True]):
print(type(i))
print(np.array([[1,2,3],[1,2,3],[1,2,3]]).sum())
print(Temps.sum())
print(Temps.std())
print(Temps.mean())
Find the highest temperature of the day and its corresponding time
import numpy as np
import pandas as pd
print(type(np.array([1, 2, 3])))
print(type(pd.Series([1, 2, 3])))
hours = np.array(['12AM', '1AM', '2AM', '3AM',
'4AM', '5AM', '6AM', '7AM',
'8AM', '9AM', '10AM', '11AM',
'1PM', '2PM', '3PM', '4PM',
'5PM', '6PM', '7PM', '8PM',
'9PM', '10PM', '11PM', '12PM'])
Temps = np.array([-9, 20, -15, -16,
-1, -7, 7, 8,
-13, -5, -14, -1,
-12, -1, -6, -1,
-2, 6, -1, 4,
-8, -1, -1, -3,
])
def getMaxTemp(hours,temps):
max_temp = temps.max()
max_hour = hours[temps.argmax()]
return (max_temp,max_hour)
print(getMaxTemp(hours,Temps))