我为每一列创建了不同的 bin,并根据这些对 DataFrame 进行分组。
\nimport pandas as pd\nimport numpy as np\n\nnp.random.seed(100)\ndf = pd.DataFrame(np.random.randn(100, 4), columns=[\'a\', \'b\', \'c\', \'value\'])\n\n# for simplicity, I use the same bin here\nbins = np.arange(-3, 4, 0.05)\n\ndf[\'a_bins\'] = pd.cut(df[\'a\'], bins=bins)\ndf[\'b_bins\'] = pd.cut(df[\'b\'], bins=bins)\ndf[\'c_bins\'] = pd.cut(df[\'c\'], bins=bins)\nRun Code Online (Sandbox Code Playgroud)\n的输出df.groupby([\'a_bins\',\'b_bins\',\'c_bins\']).size() 表明组长度为2685619。
然后,计算各组的统计数据如下:
\n%%timeit\ndf.groupby([\'a_bins\',\'b_bins\',\'c_bins\']).agg({\'value\':[\'mean\']})\n\n>>> 16.9 s \xc2\xb1 637 ms per loop (mean \xc2\xb1 std. dev. of 7 runs, 1 loop each)\nRun Code Online (Sandbox Code Playgroud)\na, b, and c值来查找值,如下所示:我有三个一维数组:
idxs: 索引数据weights: 中每个指标的权重 idxsbins:用于计算其中最小重量的 bin。这是我当前使用的方法idxs来检查weights在哪个 bin 中调用的数据,然后计算 bin 权重的最小值/最大值:
slices显示每个垃圾箱idxs元素所属的。slices和weights同时。weights每个 bin(切片)中的最小值。import random
import numpy as np
# create example data
out_size = int(10)
bins = np.arange(3, out_size-3)
idxs = np.arange(0, out_size)
#random.shuffle(idxs)
# set duplicated slice manually for test
idxs[4] = idxs[3]
idxs[6] = idxs[7]
weights = idxs
# get which bin idxs belong …Run Code Online (Sandbox Code Playgroud) 使用此代码,我不知道如何自定义颜色栏。这个网站上的颜色图不能满足我。
shade = m.contourf(Lon,Lat,TBB,np.arange(-90, -20, 10),extend='both',cmap=plt.cm.get_cmap('jet'))
m.colorbar(shade)
Run Code Online (Sandbox Code Playgroud)
我有一个二维数组,想要按列获取所有唯一数字的出现次数。
这是一个例子:
import numpy as np
a = np.array([[2,2,3,3],
[2,3,3,3],
[3,3,4,4]])
Run Code Online (Sandbox Code Playgroud)
结果应该是
[[2,1,0,0],
[1,2,2,2],
[0,0,1,1]])
Run Code Online (Sandbox Code Playgroud)
例如,第一行是2每列中出现的数字,0 表示2不在第三和第四列中。第二行是数字的出现3,最后一行是数字的出现4。简而言之,我想获取每个排序的唯一值的每列计数。
我尝试过np.unique(a, return_counts=True, axis=0),但得到了错误的结果:
(array([[2, 2, 3, 3],
[2, 3, 3, 3],
[3, 3, 4, 4]]),
array([1, 1, 1]))
Run Code Online (Sandbox Code Playgroud) 我有两个一维数组,想将它们组合成一个 Point GeoSeries,如下所示:
import numpy as np
from geopandas import GeoSeries
from shapely.geometry import Point
x = np.random.rand(int(1e6))
y = np.random.rand(int(1e6))
GeoSeries(map(Point, zip(x, y)))
Run Code Online (Sandbox Code Playgroud)
在我的笔记本电脑上大约需要 5 秒。是否可以加速GeoSeries的生成?