
numpy/pandas/matplotlib
文章平均质量分 69
numpy/pandas/matplotlib
华间一壶酒
这个作者很懒,什么都没留下…
展开
-
pandas,numpy杂记
df加前缀df.add_prefix(‘prefix’)原创 2021-02-24 13:48:13 · 172 阅读 · 0 评论 -
10 groupby快速上手
10 groupby快速上手本文的目的在于指导读者快速上手groupby。在每个知识点下,我会先说明用法,再给出示例。本文只给出了基本的用法,想要获得更详尽的内容请参阅文档:Group by: split-apply-combine10.1 分组分组依据可以是:函数,会在各个索引值上被调用一次,其返回值就会被用作分组名称numpy array字典或Series列名以上类型的混合组成的列表示例In [1]: df = pd.DataFrame( ...: [ ..原创 2021-02-22 22:45:40 · 248 阅读 · 0 评论 -
9.使用groupby对数据分组和计算组内统计量(不完善)
9.使用groupby对数据分组和计算组内统计量经常会遇到这样的场景:首先把数据分组,然后处理下组内数据,最后把处理结果组合起来。groupby就可以满足需求。9.1 分组只要提供一个分组依据,groupby就可以对数据分组了。In [1]: df = pd.DataFrame( ...: [ ...: ("bird", "Falconiformes", 389.0), ...: ("bird", "Psittaciformes", 24.0原创 2021-02-08 12:35:58 · 2422 阅读 · 0 评论 -
8. 比较concat, append, merge, join
8. 比较concat, append, merge, join 连接DataFrame8.1 比较连接方向concat可以横向纵向连接对象;appned是纵向连接对象;merge和join是横向连接对象。连接对象concat,append一次可以连接多个对象,可以是多个Series、DataFrame或者2者混合merge,join一次只能连接2个append可以为DF追加行,要求行是Series,DF或字典连接方式concat左右连接时,不能指定连接键,只能使用in原创 2020-12-28 17:44:29 · 552 阅读 · 1 评论 -
7. np.where, np.argwhere, df.where, df.mask
7. np.where, np.argwhere, df.where, df.mask7.1 np.wherenumpy.where(condition[, x, y])根据条件选择x或y的元素返回。如果condition为真,返回x的元素;反之返回y的元素a = np.arange(10)aarray([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])np.where(a < 5, a, 10*a)array([ 0, 1, 2, 3, 4, 50, 60,原创 2020-12-22 22:18:42 · 927 阅读 · 0 评论 -
3. 在折线图中把个别点圈出
3. 在折线图中把个别点圈出思路是在这一点画散点图,把散点图设置为空心圆。import matplotlib.pyplot as pltimport numpy as npx,y=np.arange(5),np.arange(5)fig,ax=plt.subplots()ax.plot(x,y)ax.scatter(x[3],y[3] ,color='', marker='o', edgecolors='g', s=200)# color设为空,用edgecolors控制圆圈颜色,s控制圆圈原创 2020-11-27 21:35:42 · 1399 阅读 · 0 评论 -
numpy 分母为零的处理办法
当除数数组中包含0时https://stackoverflow.com/questions/26248654/how-to-return-0-with-divide-by-zerohttps://blog.youkuaiyun.com/minhuaqaq/article/details/104773022?biz_id=102&utm_term=numpy%20%E5%88%86%E6%AF%8D%E4%B8%BA0&utm_medium=distribute.pc_search_result.no转载 2020-11-21 16:40:26 · 16360 阅读 · 1 评论 -
numpy,pandas求最大值索引
numpynumpy.argmax(a, axis=None, out=None)如果数组有多个最大值,只会返回第一个的索引。axis int, optionalBy default, the index is into the flattened array, otherwise along the specified axis.如果不给axis传值,那么默认把数组拉平,返回拉平后的索引。a=np.arange(10).reshape(5,2)a[1,1]=9print(a)#arra原创 2020-11-19 23:01:52 · 3281 阅读 · 0 评论 -
numpy , pandas 划分bins
numpy 中划分bins,并计算一个bin内的均值import numpydata = np.array([range(100)])bins = numpy.linspace(0, 50, 10)bins=np.append(bins,np.inf)#最后一个bin到无穷大digitized = numpy.digitize(data, bins)#Return the indices of the bins to which each value in input array belongs.原创 2020-11-19 21:52:08 · 6741 阅读 · 1 评论 -
python 对数组(numpy, pandas, list)中的每个元素进行操作
numpy中使用apply_along_axis:numpy.apply_along_axis(func1d, axis, arr, *args, **kwargs)In [23]: b = np.array([[8,1,7], [4,3,9], [5,2,6]])In [24]: np.apply_along_axis(sorted, 1, b)Out[24]: array([[1, 7, 8], [3, 4, 9], [2, 5, 6]])在pandas中使用原创 2020-11-16 20:47:05 · 9034 阅读 · 0 评论 -
matplotlib绘图,基本绘图,设置子图间距,设置折线类型,标签,设置坐标轴范围、标签,设置子图标题
基本绘图,设置子图间距,设置折线类型,标签,设置坐标轴范围、标签,设置子图标题import matplotlib.pyplot as pltimport numpy as npfrom sklearn.linear_model import LinearRegressionx1,y1=[-1,0,1],[0,0,1]x2,y2=np.arange(5),np.random.rand(5,1)x3,n_bins=np.random.randn(200,1),50x4,y4=np.random原创 2020-11-06 15:41:51 · 1382 阅读 · 0 评论 -
pandas中把字符串转为时序数据,时序数据转float
X['Time']=pd.to_datetime(X['Time'])X['UnixTime']=X.apply(lambda row:row['Time'].timestamp(), axis=1)原创 2020-11-17 20:13:34 · 556 阅读 · 0 评论 -
按行、列连接numpy数组
我最先知道的是np.c[]可以按列连接2个数组,后来我要按行连接数组,于是我就传入axis=0,发现不行,查过文档后得知原来有专门的np.r_[]np.r_[np.array([1,2,3]), 0, 0, np.array([4,5,6])]#array([1, 2, 3, ..., 4, 5, 6])np.r_[-1:1:6j, [0]*3, 5, 6]#array([-1. , -0.6, -0.2, 0.2, 0.6, 1. , 0. , 0. , 0. , 5. , 6.原创 2020-11-16 20:31:33 · 1077 阅读 · 0 评论 -
numpy中常见的random
numpy.random.rand(d0, d1, ..., dn) 返回给定形状的数组,其元素来自[0,1)均匀分布。numpy.random.randint(low, high=None, size=None, dtype=int) 返回来自[low,high)均匀分布的整数。如果没有给出,则[0,low)numpy.random.randn(d0, d1, ..., dn)返回给定形状的数组,其元素来自标准分布。numpy.random.random(size=None)返回 [0.0.原创 2020-11-05 21:14:32 · 159 阅读 · 0 评论 -
pandas中对类别属性计数、统计出现的不同类别
Series.unique()返回Series对象中唯一值的NumPy arraypd.Series([2, 1, 3, 3], name='A').unique()#array([2, 1, 3])Series.value_counts(normalize=False, sort=True, ascending=False, bins=None, dropna=True)返回一个Series,记录Series对象中唯一值出现的次数。index = pd.Index([3, 1, 2, 3, 4,原创 2020-11-17 16:46:05 · 3017 阅读 · 0 评论