numpy关于两列进行排序
时间: 2023-07-20 11:24:37 浏览: 60
你可以使用`argsort()`函数来对 numpy 数组中的两列进行排序。
假设你有一个二维数组 `arr`,其中有两列需要排序,你可以这样做:
``` python
import numpy as np
# 创建一个随机数组
arr = np.random.randint(0, 10, size=(5, 2))
print("原始数组:\n", arr)
# 对第一列进行排序
sorted_indices = arr[:, 0].argsort()
sorted_arr = arr[sorted_indices]
print("按第一列排序后的数组:\n", sorted_arr)
# 对第二列进行排序
sorted_indices = arr[:, 1].argsort()
sorted_arr = arr[sorted_indices]
print("按第二列排序后的数组:\n", sorted_arr)
```
以上代码将输出类似以下的结果:
```
原始数组:
[[6 0]
[4 8]
[3 0]
[6 4]
[3 3]]
按第一列排序后的数组:
[[3 0]
[3 3]
[4 8]
[6 0]
[6 4]]
按第二列排序后的数组:
[[6 0]
[3 0]
[3 3]
[6 4]
[4 8]]
```
这里使用了 `argsort()` 函数得到了排序后的索引,然后使用索引重新排列原始数组。
相关问题
numpy关于两列进行排序,两列分别是主序和附序
如果你需要按照两列排序,其中一列为主序列,另一列为附序列,则可以先按照主序列排序,然后在主序列相同的情况下按照附序列排序。以下是一个例子:
``` python
import numpy as np
# 创建一个随机数组
arr = np.random.randint(0, 10, size=(5, 2))
print("原始数组:\n", arr)
# 先按照第一列排序,再按照第二列排序
sorted_indices = np.lexsort((arr[:, 1], arr[:, 0]))
sorted_arr = arr[sorted_indices]
print("按照两列排序后的数组:\n", sorted_arr)
```
以上代码将输出类似以下的结果:
```
原始数组:
[[2 5]
[8 6]
[6 4]
[5 8]
[3 4]]
按照两列排序后的数组:
[[6 4]
[3 4]
[2 5]
[8 6]
[5 8]]
```
这里使用了 `lexsort()` 函数,它可以按照多个键对数组进行排序,其中最后一个键为主键,依次向前为附键。因此,我们先将第二列作为最后一个键(附键),第一列作为倒数第二个键(主键),这样就可以实现按照两列排序了。
基于iris数据集的统计分析 3 实验目的: 1 掌握函数的定义和调用、及Python文件操作基础。 2 熟悉掌握Numpy数值计算;熟悉Numpy的数据处理方法。 3 掌握Numpy中的数据统计与分析排序、数据去重,常用统计分析方法。 4实验环境: Anaconda3 5实验内容与要求: 本实训读取iris数据集中鸢尾花的萼片、花瓣长度数据,并对其进行排序,去重,并求出和、累计和、均值、标准差、方差、最小值、最大值。 1、导入模块,获取数据,显示前5行 2、数据清理,转换为数组 3、获取Petal.Length列,转换为浮点数,并输出前5个数 4、对Petal.Length列进行排序、去重 5、求出Petal.length列的和、均值、标准差、方差、最小值、最大值 6、开放式题目:任选两列进行任意统计分析(如四则运算、数组变换、统计分析等)
以下是基于iris数据集的统计分析3的代码实现:
```python
import numpy as np
# 1. 导入数据,显示前5行
with open("iris.data", "r") as f:
data = f.readlines()
for i in range(5):
print(data[i])
# 2. 数据清理,转换为数组
data_array = np.genfromtxt("iris.data", delimiter=",", usecols=(0, 1, 2, 3))
data_array = np.delete(data_array, np.s_[150:], 0)
# 3. 获取Petal.Length列,转换为浮点数,并输出前5个数
petal_length = data_array[:, 2].astype(np.float)
print("Petal.Length前5个数:", petal_length[:5])
# 4. 对Petal.Length列进行排序、去重
sorted_petal_length = np.sort(petal_length)
unique_petal_length = np.unique(petal_length)
# 5. 求出Petal.length列的和、均值、标准差、方差、最小值、最大值
petal_length_sum = np.sum(petal_length)
petal_length_mean = np.mean(petal_length)
petal_length_std = np.std(petal_length)
petal_length_var = np.var(petal_length)
petal_length_min = np.min(petal_length)
petal_length_max = np.max(petal_length)
print("Petal.Length列的和:", petal_length_sum)
print("Petal.Length列的均值:", petal_length_mean)
print("Petal.Length列的标准差:", petal_length_std)
print("Petal.Length列的方差:", petal_length_var)
print("Petal.Length列的最小值:", petal_length_min)
print("Petal.Length列的最大值:", petal_length_max)
# 6. 任选两列进行任意统计分析
# 选取Petal.Length和Sepal.Length列进行四则运算
sepal_length = data_array[:, 0].astype(np.float)
result = petal_length + sepal_length
print("Petal.Length和Sepal.Length列进行四则运算结果:", result)
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)