按列Python进行数字排序

时间:2015-03-02 16:28:58

标签: python sorting pandas

我正在尝试按“DayOfYear”列进行数字排序,但它不允许我这样做。我将它转换为带有pandas的数据框,但我对数字排序没有运气。

  results7 = ([['1', '660.206159375', '2200.969', '1588.2815', '831.8674',
    '521.3459', '329.77015', '205.89374', '102.9523'],
   ['10', '932.475360938', '2381.846', '2098.8083', '1289.0055',
    '780.2308', '522.78565', '340.15073', '298.4486'],
   ['11', '914.067898437', '2698.957', '1922.7277', '1259.1625',
    '731.6858', '505.171', '267.1976', '203.4595'],
   ['12', '786.08815', '2580.748', '1823.5057', '908.63865',
    '652.7224', '455.6254', '229.1738', '146.3479'],
   ['2', '928.879345312', '3075.417', '2560.8875', '1166.789',
    '734.7388', '512.9898', '241.98641', '167.2151'],
  ['', nan, nan, nan, nan, nan, nan, nan, nan]], dtype=object
   ])

names50 = ['DayOfYear','AvgFlow','Max','n95th','n75th','n50th','n25th','n5th','Min']
results9 = pd.DataFrame(results8,columns=names50)
results10 = results9.astype(str).convert_objects(convert_numeric=True)

results11 = results10[~np.isnan(results10).any(axis = 1)]
results12 = np.sort(results11, order = 'DayOfYear')

我收到了

的错误消息
  

ValueError:当数组没有字段时,无法指定顺序。

将其转换为pd.DataFrame时,是否未指定字段?此外,当我尝试执行.dtype时,我收到一条错误消息:

  

AttributeError:'DataFrame'对象没有属性'dtype'

我在这里做错了什么?

1 个答案:

答案 0 :(得分:1)

1)使用DataFrames dtypes属性。它会为每个dtype返回Series属性。

http://pandas.pydata.org/pandas-docs/dev/generated/pandas.DataFrame.dtypes.html#pandas.DataFrame.dtypes

2)只需使用pandas的排序......

results11.sort('DayOfYear')