如果A是2x2数组,那么permute(A, [3 2 1])在MATLAB 中python中的等价表达式是什么?
谢谢
我有以下类型的数组:
a = array([[1,1,1],
[1,1,1],
[1,1,1],
[2,2,2],
[2,2,2],
[2,2,2],
[3,3,0],
[3,3,0],
[3,3,0]])
Run Code Online (Sandbox Code Playgroud)
我想计算每种类型数组的出现次数,例如
[1,1,1]:3, [2,2,2]:3, and [3,3,0]: 3
Run Code Online (Sandbox Code Playgroud)
我怎么能在python中实现这一点?是否可以不使用for循环并计入字典?它必须快速,并且应该花费不到0.1秒左右.我查看了Counter,numpy bincount等等.但是,这些是针对个别元素而不是数组.
谢谢.
我一直在尝试在密码查询中编写以下任务,但我没有得到正确的结果.其他stackoverflow问题讨论限制或收集,但我认为这不足以完成以下任务.
任务:我有(p:Product)节点,在两个产品节点之间有一个名为"BOUGHT_TOGETHER"的关系.那是
(p:Product)-[b:BOUGHT_TOGETHER]-(q:Product)
Run Code Online (Sandbox Code Playgroud)
并且关系b具有称为"大小"的属性,其包含一些数字.我想返回按大小排序的每个产品关系的前3个结果.例如,查询结果应如下所示.
+------------------------+
| p.id | q.id | b.size |
+------------------------+
1 2 10
1 3 8
1 5 7
2 21 34
2 17 20
2 35 15
3 5 49
3 333 30
3 65 5
. . .
. . .
. . .
Run Code Online (Sandbox Code Playgroud)
有人可以告诉我如何编写一个密码查询,以达到预期的效果吗?谢谢!
我有一个数据框如下:
raw_data = {'regiment': ['Nighthawks', 'Nighthawks', 'Nighthawks', 'Nighthawks', 'Dragoons', 'Dragoons', 'Dragoons', 'Dragoons', 'Scouts', 'Scouts', 'Scouts', 'Scouts'],
'company': ['1st', '1st', '2nd', '2nd', '1st', '1st', '2nd', '2nd','1st', '1st', '2nd', '2nd'],
'name': ['Miller', 'Jacobson', 'Ali', 'Milner', 'Cooze', 'Jacon', 'Ryaner', 'Sone', 'Sloan', 'Piger', 'Riani', 'Ali'],
'preTestScore': [4, 24, 31, 2, 3, 4, 24, 31, 2, 3, 2, 3],
'postTestScore': [25, 94, 57, 62, 70, 25, 94, 57, 62, 70, 62, 70]}
df = pd.DataFrame(raw_data, columns = ['regiment', 'company', 'name', 'preTestScore', 'postTestScore'])
Run Code Online (Sandbox Code Playgroud)
如果我按两列分组并计算大小,
df.groupby(['regiment','company']).size()
Run Code Online (Sandbox Code Playgroud)
我得到以下信息: …
我有一个多列的数据框.前两列是x和y坐标,其余列是(x,y)对的不同属性值.
import pandas as pd
import numpy as np
df = pd.DataFrame()
df['x'] = np.random.randint(1,1000,100)
df['y'] = np.random.randint(1,1000,100)
df['val1'] = np.random.randint(1,1000,100)
df['val2'] = np.random.randint(1,1000,100)
df['val3'] = np.random.randint(1,1000,100)
print df.head()
x y val1 val2 val3
0 337 794 449 969 933
1 19 563 592 677 886
2 512 467 664 160 16
3 36 112 91 230 910
4 972 572 336 879 860
Run Code Online (Sandbox Code Playgroud)
在Bokeh中使用customJS,我想通过提供下拉菜单来更改二维热图中的颜色值.
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource
from bokeh.models import LinearColorMapper
from bokeh.palettes import …Run Code Online (Sandbox Code Playgroud) 如果单词中的前两个字母用相同的字母重复,如何替换?
例如,
string = 'hhappy'
Run Code Online (Sandbox Code Playgroud)
我想得到
happy
Run Code Online (Sandbox Code Playgroud)
我试过了
re.sub(r'(.)\1+', r'\1', string)
Run Code Online (Sandbox Code Playgroud)
但是,这给了
hapy
Run Code Online (Sandbox Code Playgroud)
谢谢!
我在R中看到了解决方案但在python中没有看到.如果问题重复,请指出我之前提出的问题/解决方案.
我有一个数据帧如下.
df = pd.DataFrame({'col1': ['a','b','c','c','d','e','a','h','i','a'],'col2':['3:00','3:00','4:00','4:00','3:00','5:00','5:00','3:00','3:00','2:00']})
df
Out[83]:
col1 col2
0 a 3:00
1 b 3:00
2 c 4:00
3 c 4:00
4 d 3:00
5 e 5:00
6 a 5:00
7 h 3:00
8 i 3:00
9 a 2:00
Run Code Online (Sandbox Code Playgroud)
我想做的是groupby'col1'并为col2中的不同值分配一个唯一的ID,如下所示:
col1 col2 ID
a 2:00 0
a 3:00 1
a 5:00 2
b 3:00 0
c 4:00 0
c 4:00 0
...
Run Code Online (Sandbox Code Playgroud)
我尝试使用pd.Categorical,但不能完全达到我想要的位置.非常感谢任何帮助.谢谢.
最初,我创建了英国邮政编码区域的交互式地图,其中单个区域根据其值(例如该邮政编码区域中的人口)用颜色表示,如下所示。
from bokeh.plotting import figure
from bokeh.palettes import Viridis256 as palette
from bokeh.models import LinearColorMapper
from bokeh.models import ColumnDataSource
import geopandas as gpd
shp = 'file_path_to_the_downloaded_shapefile'
#read shape file into dataframe using geopandas
df = gpd.read_file(shp)
def expandMultiPolygons(row, geometry):
if row[geometry].type = 'MultiPolygon':
row[geometry] = [p for p in row[geometry]]
return row
#Some rows were in MultiPolygons instead of Polygons.
#Expand MultiPolygons to multi rows of Polygons
df = df.apply(expandMultiPolygons, geometry='geometry', axis=1)
df = df.set_index('Area')['geometry'].apply(pd.Series).stack().reset_index()
#Visualize the polygons. To visualize …Run Code Online (Sandbox Code Playgroud) python ×6
bokeh ×2
numpy ×2
pandas ×2
arrays ×1
cypher ×1
datashader ×1
dictionary ×1
group-by ×1
matlab ×1
multi-index ×1
neo4j ×1
octave ×1
python-2.7 ×1
regex ×1