我们都知道,Numpy 是 Python 环境下的扩展程序库,支持大量的维度数组和矩阵运算;Pandas 也是 Python 环境下的数据操作和分析软件包,以及强大的数据分析库。二者在日常的数据分析中都发挥着重要作用,如果没有 Numpy 和 Pandas 的支持,数据分析将变得异常困难。但有时我们需要加快数据分析的速度,有什么办法可以帮助到我们吗?
在本文中,数据和分析工程师 Kunal Dhariwal 为我们介绍了 12 种 Numpy 和 Pandas 函数,这些高效的函数会令数据分析更为容易、便捷。最后,读者也可以在 GitHub 项目中找到本文所用代码的 Jupyter Notebook。
x = np.array([12, 10, 12, 0, 6, 8, 9, 1, 16, 4, 6, 0])index_val = np.argpartition(x, -4)[-4:]index_valarray([1, 8, 2, 0], dtype=int64)np.sort(x[index_val])array([10, 12, 12, 16])复制代码
array1 = np.array([0.12,0.17,0.24,0.29])array2 = np.array([0.13,0.19,0.26,0.31])# with a tolerance of 0.1, it should return False:np.allclose(array1,array2,0.1)False# with a tolerance of 0.2, it should return True:np.allclose(array1,array2,0.2)True复制代码
x = np.array([3, 17, 14, 23, 2, 2, 6, 8, 1, 2, 16, 0])np.clip(x,2,5)array([3, 5, 5, 5, 2, 2, 5, 5, 2, 2, 5, 2])复制代码
# Random integersarray = np.random.randint(20, size=12)arrayarray([ 0, 1, 8, 19, 16, 18, 10, 11, 2, 13, 14, 3])# Divide by 2 and check if remainder is 1cond = np.mod(array, 2)==1condarray([False, True, False, True, False, False, False, True, False, True, False, True])# Use extract to get the valuesnp.extract(cond, array)array([ 1, 19, 11, 13, 3])# Apply condition on extract directlynp.extract(((array < 3) | (array > 15)), array)array([ 0, 1, 19, 16, 18, 2])复制代码
y = np.array([1,5,6,8,1,7,3,6,9])# Where y is greater than 5, returns index positionnp.where(y>5)array([2, 3, 5, 7, 8], dtype=int64),)# First will replace the values that match the condition, # second will replace the values that does notnp.where(y>5, "Hit", "Miss")array(['Miss', 'Miss', 'Hit', 'Hit', 'Miss', 'Hit', 'Miss', 'Hit', 'Hit'],dtype='<U4')复制代码
a = np.array([1,5,6,8,1,7,3,6,9])print("50th Percentile of a, axis = 0 : ", np.percentile(a, 50, axis =0))50th Percentile of a, axis = 0 : 6.0b = np.array([[10, 7, 4], [3, 2, 1]])print("30th Percentile of b, axis = 0 : ", np.percentile(b, 30, axis =0))30th Percentile of b, axis = 0 : [5.1 3.5 1.9]复制代码
-
具有异构类型列的表格数据,如 SQL 表或 Excel 表;
-
有序和无序 (不一定是固定频率) 的时间序列数据;
-
带有行/列标签的任意矩阵数据(同构类型或者是异构类型);
-
其他任意形式的统计数据集。事实上,数据根本不需要标记就可以放入 Pandas 结构中。
-
容易处理浮点数据和非浮点数据中的 缺失数据(用 NaN 表示);
-
大小可调整性: 可以从 DataFrame 或者更高维度的对象中插入或者是删除列;
-
显式数据可自动对齐: 对象可以显式地对齐至一组标签内,或者用户可以简单地选择忽略标签,使 Series、 DataFrame 等自动对齐数据;
-
灵活的分组功能,对数据集执行拆分-应用-合并等操作,对数据进行聚合和转换;
-
简化将数据转换为 DataFrame 对象的过程,而这些数据基本是 Python 和 NumPy 数据结构中不规则、不同索引的数据;
-
基于标签的智能切片、索引以及面向大型数据集的子设定;
-
更加直观地合并以及连接数据集;
-
更加灵活地重塑、转置(pivot)数据集;
-
轴的分级标记 (可能包含多个标记);
-
具有鲁棒性的 IO 工具,用于从平面文件 (CSV 和 delimited)、 Excel 文件、数据库中加在数据,以及从 HDF5 格式中保存 / 加载数据;
-
时间序列的特定功能: 数据范围的生成以及频率转换、移动窗口统计、数据移动和滞后等。
import ioimport requests# I am using this online data set just to make things easier for you guysurl = "https://raw.github.com/vincentarelbundock/Rdatasets/master/csv/datasets/AirPassengers.csv"s = requests.get(url).content# read only first 10 rowsdf = pd.read_csv(io.StringIO(s.decode('utf-8')),nrows=10 , index_col=0)复制代码