简体   繁体   中英

numeric sort by column Python

I am trying to do a numeric sort by column "DayOfYear' but it is not allowing me to. I converted it into a dataframe with pandas but I have had no luck with the numeric sorting.

  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')

I received an error message of

ValueError: Cannot specify order when the array has no fields.

Did I not specify the fields when I converted it to a pd.DataFrame? Also when I try to do a .dtype I receive an error message:

AttributeError: 'DataFrame' object has no attribute 'dtype'

What am I doing wrong here?

1) Use the DataFrames dtypes attribute. It returns the dtype attribute for each of its Series .

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

2) Just use pandas' sort...

results11.sort('DayOfYear')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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