简体   繁体   English

如何对列进行排序并将它们分组在熊猫上?

[英]how to sort a column and group them on pandas?

I am new on pandas. 我是大熊猫新手。 I try to sort a column and group them by their numbers. 我尝试对一列进行排序,并按其编号对其进行分组。

df = pd.read_csv("12Patients150526 mutations-ORIGINAL.txt", sep="\t", header=0)
samp=df["SAMPLE"]

samp
Out[3]: 
0        11
1         2
2         9
3         1
4         8
5         2
6         1     
7         3
8        10
9         4
10        5
     ..
53157    12
53158     3
53159     2
53160    10
53161     2
53162     3
53163     4
53164    11
53165    12
53166    11
Name: SAMPLE, dtype: int64

#sorting
grp=df.sort(samp)

This code does not work. 此代码不起作用。 Can somebody help me about my problem, please. 请有人帮我解决我的问题。

How can I sort and group them by their numbers? 如何按编号对它们进行排序和分组?

To sort df based on a particular column, use df.sort() and pass column name as parameter. 要基于特定列对df进行排序,请使用df.sort()并将列名作为参数传递。

import pandas as pd
import numpy as np

# data
# ===========================
np.random.seed(0)
df = pd.DataFrame(np.random.randint(1,10,1000), columns=['SAMPLE'])
df

     SAMPLE
0         6
1         1
2         4
3         4
4         8
5         4
6         6
7         3
..      ...
992       3
993       2
994       1
995       2
996       7
997       4
998       5
999       4

[1000 rows x 1 columns]

# sort
# ======================
df.sort('SAMPLE')

     SAMPLE
310       1
710       1
935       1
463       1
462       1
136       1
141       1
144       1
..      ...
174       9
392       9
386       9
382       9
178       9
772       9
890       9
307       9

[1000 rows x 1 columns]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM