[英]Sorting Column in Pandas dataframe-Python
I have a dataframe that looks like this:我有一个看起来像这样的 dataframe:
class_id班级号 | dims暗淡 |
---|---|
94369_GA_30122 94369_GA_30122 | 95 95 |
27369_GA_30122 27369_GA_30122 | 14 14 |
78369_CA_30122 78369_CA_30122 | 27 27 |
30472_MN_55121 30472_MN_55121 | 16 16 |
and the dataframe goes on... I want to sort my column class_id numerically ascending, that is itt must look like和 dataframe 继续......我想按数字升序对我的列 class_id 进行排序,它必须看起来像
class_id班级号 | dims暗淡 |
---|---|
27369_GA_30122 27369_GA_30122 | 14 14 |
30472_MN_55121 30472_MN_55121 | 16 16 |
78369_CA_30122 78369_CA_30122 | 27 27 |
94369_GA_30122 94369_GA_30122 | 95 95 |
can anyone tell me how can I achieve this?谁能告诉我我怎样才能做到这一点?
I believe this should do the trick:我相信这应该可以解决问题:
data = {"class_id": ["94369_GA_30122", "27369_GA_30122", "78369_CA_30122", "30472_MN_55121"],
"dims": [95, 14, 27, 16]}
df = pd.DataFrame(data)
df = df.sort_values("class_id")
Out:
class_id dims
1 27369_GA_30122 14
3 30472_MN_55121 16
2 78369_CA_30122 27
0 94369_GA_30122 95
Edit: You can also add these lines to only sort on the first set of numbers.编辑:您还可以添加这些行以仅对第一组数字进行排序。
df["sorting"] = df["class_id"].str.split("_", n=1).str[0] # Extracting only the first set of numbers
df = df.sort_values("sorting")
df = df.drop("sorting", axis=1) # To drop the column again
https://pandas.pydata.org/docs/reference/api/pandas.Series.str.split.html https://pandas.pydata.org/docs/reference/api/pandas.Series.str.split.html
If you want to sort the value by class_id:如果要按 class_id 对值进行排序:
df.sort_values(by=['class_id'])
If you want to sort the value by dims:如果要按暗度对值进行排序:
df.sort_values(by=['dims'])
Tf you want to sort based on both you can use: Tf 你想根据两者进行排序,你可以使用:
df.sort_values(by=['class_id', 'dims'])
you can refer from this site - https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.sort_values.html你可以参考这个网站 - https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.sort_values.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.