Python Pandas如何读取json而不按索引排序?

时间:2016-10-24 03:00:22

标签: pandas dataframe

如果我有这样的json文件,并将其导入到dataframe,则列顺序始终为sort -0.8,-0.9。我希望订单维持,因为它在json中定义为-0.9,-0.8

- (void)showNotification:(NSString *)text {
  UIAlertController* avc = [UIAlertController alertControllerWithTitle:@"Success" message:text
                                                        preferredStyle:UIAlertControllerStyleAlert];
  UIAlertAction *ok = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleCancel handler:nil];
 [avc addAction:ok];

  [self performSelector:@selector(dissmissAlert:) withObject:avc afterDelay:3.0];

  [self.window.rootViewController presentViewController:avc animated:true completion:nil];
}
-(void)dissmissAlert:(UIAlertController *) alert{
  [alert dismissViewControllerAnimated:true completion:nil];
}

1 个答案:

答案 0 :(得分:2)

您可以将您的json数据加载为OrderedDict以保留键的顺序,然后使用DataFrame.from_dict构造函数:

import json
from collections import OrderedDict

s = """{
    "-0.90": {
        "A": 1.0,
        "B": 0.4935585804
    },
    "-0.80": {
        "A": 1.0,
        "B": 0.4935585804
    }
}"""

data = json.loads(s, object_pairs_hook=OrderedDict)
pd.DataFrame.from_dict(data)

      -0.90     -0.80
A  1.000000  1.000000
B  0.493559  0.493559