简体   繁体   English

如何使用rangeindex [pandas]对数据框进行排序

[英]How to sort a dataframe with rangeindex [pandas]

Just started to learn python for data processing. 刚开始学习python进行数据处理。 Sorry if this is too basic, but I really couldn't find a solution. 抱歉,这太基础了,但是我真的找不到解决方案。 I created a DataFrame from a list of tuples: 我从元组列表创建了一个DataFrame:

A=[(0, 1, 0.0),
   (88, 2, 8.3),
   (89, 2, 5.5),
   (96, 2, 7)]
df = pd.DataFrame(A)

Now I got a dataframe with RangeIndex. 现在我有了一个带有RangeIndex的数据框。 How could I sort the data by one column? 如何按一列对数据排序? I found a lot of tutorial and examples, eg, df.sort_values(by=['col']) , this works with named indexes or columns. 我发现了很多教程和示例,例如df.sort_values(by=['col']) ,它可用于命名索引或列。 But since the df doesn't have a name, how do I specify it in the sort_values method? 但是,由于df没有名称,如何在sort_values方法中指定它? Or how do I sort the dataframe by the third column? 或者如何按第三列对数据框进行排序?

I did exactly as you did, and sort_values worked fine for me. 我做的和您做的完全一样, sort_values对我来说很好。 In fact, I didn't have a RangeIndex; 实际上,我没有RangeIndex。 just a normal pandas dataframe. 只是一个普通的熊猫数据框。 Here's my code: 这是我的代码:

import pandas as pd
x = [(6,5,4), (3,2,1)]
df = pd.DataFrame(x)
print(df) # View the unsorted dataframe
df.sort_values(by=0) # Sorts by the 0th column
print(df) # View the sorted dataframe

Even though my columns aren't named, they are still indexed, so I can refer to them by their number when sorting. 即使我的列未命名,它们仍然被索引,因此在排序时我可以通过它们的编号来引用它们。

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

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