如果你用熊猫,这很容易。在import pandas as pd
def sorted_df(df, ascending=False):
grouped = df.groupby([0,1])
data = []
for g in grouped:
d = g[1]
d[4] = d[2].rank(ascending=ascending)
d = d.sort(4)
data.append(d)
return pd.concat(data)
# load our dataframe from a csv string
import StringIO
f = StringIO.StringIO("""uniquedata1,uniquecell1,42,data,1,data
uniquedata1,uniquecell1,32,data,2,data
uniquedata1,uniquecell1,13,data,3,data
uniquedata2,uniquecell2,41,data,2,data
uniquedata2,uniquecell2,39,data,3,data
uniquedata2,uniquecell2,45,data,1,data
uniquedata2,uniquecell2,22,data,4,data
uniquedata1,uniquecell2,36,data,3,data
uniquedata1,uniquecell2,66,data,1,data
uniquedata1,uniquecell2,40,data,2,data""")
df