sort_values怎么按列排序一个list数组
时间: 2024-05-15 13:19:49 浏览: 89
sort_values是pandas中的函数,用于对DataFrame或Series按照某一列或多列进行排序。如果要对一个list数组进行排序,可以使用Python内置的sorted函数。
例如,如果要按照列表中的第二个元素进行排序,可以使用以下代码:
```
my_list = [[1, 3], [2, 1], [3, 2]]
sorted_list = sorted(my_list, key=lambda x: x[1])
```
这里的key参数指定了排序的方式,lambda函数返回每个元素的第二个值作为排序依据。排序结果为:
```
[[2, 1], [3, 2], [1, 3]]
```
如果要按照第一个元素进行排序,则lambda函数可以改为x[0]。
相关问题
AttributeError: 'float' object has no attribute 'sort_values'
这个错误通常是因为你正在尝试对一个浮点数对象使用sort_values()方法,但是该方法只能用于pandas DataFrame和Series对象。如果你想对一个列表或数组进行排序,可以使用Python内置的sorted()函数或numpy库中的sort()函数。以下是一个使用sorted()函数对列表进行排序的例子:
```python
my_list = [3.2, 1.5, 4.8, 2.7]
sorted_list = sorted(my_list)
print(sorted_list) # 输出:[1.5, 2.7, 3.2, 4.8]
```
node_list = pd.concat([temp['source'],temp['target']],axis=0).sort_values().unique().tolist()是什么意思
这是一种操作,将一个DataFrame中的两列数据合并,去重并排序,最终输出一个数组。其中,temp['source']和temp['target']表示DataFrame中的两列数据。concat是用于合并数据的函数,axis=0表示按行合并,sort_values()是用于排序的函数,unique()是用于去重的函数,tolist()是将数组转化为列表的函数。该操作的目的可能是为了得到一个包含所有节点的列表。
阅读全文
相关推荐

















