来自read_json()的pandas数据帧的时间戳索引 [英] Timestamp index for pandas dataframe from read_json()

查看:270
本文介绍了来自read_json()的pandas数据帧的时间戳索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从文件data.json中读取一条包含以下内容的JSON行:

I'm reading a single JSON line from file data.json with this content:

[
  {
    "timestamp": 1436266865,
    "rates": {
      "EUR": 0.911228,
      "JPY": 122.5463,
      "AUD": 1.346118
    }
  },
  {
    "timestamp": 1436277661,
    "rates": {
      "JPY": 122.4789,
      "AUD": 1.348871,
      "EUR": 0.91433
    }
  }
]

进入大熊猫DataFrame.我想使用时间戳"作为DataFrame的索引.我是通过以下方式实现的:

into a pandas DataFrame. I want to use the "timestamp" as the DataFrame's index. I achieve this by:

df = pandas.read_json('data.json')
df.index = df['timestamp']
df.drop('timestamp', axis=1, inplace=1)

是否可以仅一行完成?

推荐答案

import pandas as pd

df = pd.read_json('data.json')
df.set_index('timestamp',inplace=True)
print(df)

这将为您的索引设置timestamp. inplace=True将阻止您执行df=df.set_index('timestamp'),默认情况下,它将删除该列.

What this will do is set timestamp to your index. inplace=True will prevent you having to do df=df.set_index('timestamp') and by default it'll drop the column.

                                                        rates
timestamp                                                    
1436266865  {'EUR': 0.9112279999999999, 'JPY': 122.5463, '...
1436277661  {'JPY': 122.4789, 'AUD': 1.348871, 'EUR': 0.91...

这篇关于来自read_json()的pandas数据帧的时间戳索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆