现有一个二维数组,我们想取出其一行,或者按照行输出
import numpy as np
# array1 ={ndarray:(50,100)}[[],[],[],...,[]]
array2 = np.array([[1,2,3],[4,5,6]]). #这是一个2*3的二维矩阵
#
apple = array2.shape
>>> apple = {tuple:2}(2,3)
tuple元组的定义 Python的元组与列表类似,
不同之处在于元组的元素不能修改。元组使用小括号,列表使用方括号。
apple[0]
>>> 2
apple[1]
>>> 3
np.max(array2)
>>> 6
np.min(array2)
>>> 1
index = np.where(array2 == 3)
>>>index = {tuple:2}{array([0]),array([2])}
import numpy as np
a=[4,5,6,7,8]
a=np.array(a)
D2=5.5
b=abs(a-D2)
bmin=min(b)
index=np.where(b==bmin)
c=a[index]
d=max(c)
print(d)
如果有两个值到目标值的距离是一样的,index就会返回一个tuple
参考
https://blog.csdn.net/weixin_46713695/article/details/126725305
import numpy as np
a = np.array([[1, 2], [3, 4], [9, 8]])
b = a.flatten()
print(b)
>>> b = {ndarray:(6,)}[1 2 3 4 9 8]
完全参考Python中numpy数组的拼接、合并
矩阵合并需要使用到的方法就是hstack()以及vstack(),这两个方法的区别就在于是通过行还是列进行合并
import numpy as np
a = np.matrix([1,2,3],[4,5,6])
b = np.matrix([9,8,7],[3,2,1])
np.hstack((a,b))
>>>
参考
https://jenrey.blog.csdn.net/article/details/117018229?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-1-117018229-blog-119107469.pc_relevant_vip_default&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-1-117018229-blog-119107469.pc_relevant_vip_default&utm_relevant_index=2
from collections import Counter
a = [29,36,57,12,79,43,23,56,28,11,14,15,16,37,24,35,17,24,33,15,39,46,52,13]
b = dict(Counter(a))
print ([key for key,value in b.items()if value > 1]) #只展示重复元素
print ({key:value for key,value in b.items()if value > 1}) #展现重复元素和重复次数
import functools, operator
nums_2d = [[1,2,3],[4,5,6,7],[8,9]]
nums_1d = functools.reduce(operator.concat, nums_2d)
print(nums_1d)
>>> [1,2,3,4,5,6,7,8,9]
或者可以用extend方法,更常规
nums_2d = [[1,2,3],[4,5,6,7],[8,9]]
nums_1d = []
for n in nums_2d:
nums_1d.extend(n)
print(nums_1d)
# 参考资料
https://blog.csdn.net/Yuki1127918/article/details/123284441
如果需求是列标签相同的只保留左边第一个,比如列名分别是 abcba,保留 abc 三列,后边两列前边已经有了,就删除,那么可以这么写:
import pandas as pd
import numpy as np
df = pd.DataFrame(np.arange(15).reshape(3, 5),
columns=[*'abcba'])
df
'''
a b c b a
0 0 1 2 3 4
1 5 6 7 8 9
2 10 11 12 13 14
'''
df.loc[:, ~df.columns.duplicated()]
'''
a b c
0 0 1 2
1 5 6 7
2 10 11 12
'''
作者:知乎用户
链接:https://www.zhihu.com/question/548513295/answer/2678050709
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
参考
https://www.cnblogs.com/math98/p/14252591.html