【问题标题】:Assign custom categories to json data - pandas将自定义类别分配给 json 数据 - pandas
【发布时间】:2017-09-05 23:39:37
【问题描述】:

为原始数据分配标签,而不是从 get_dummies 获取新的指标列。我想要这样的东西:

json_input:

[{id:100,vehicle_type:"Car", time:"2017-04-06 01:39:43", zone="A", type:"Checked"}, {id:101,vehicle_type:"Truck", time:"2017-04-06 02:35:45", zone="B", type:"Unchecked"}, {id:102,vehicle_type:"Truck", time:"2017-04-05 03:20:12", zone="A", type:"Checked"}, {id:103,vehicle_type:"Car", time:"2017-04-04 10:05:04", zone="C", type:"Unchecked"} ]

结果:

  • id、车辆类型、时间范围、区域、类型
  • 100, 0, 1, 1, 1
  • 101, 1, 1, 2, 0
  • 102、1、2、1、1
  • 103, 0, 3, 3, 0

时间戳 - TS columns -> vehicle_type, type are binary , time_range (1 -> (TS1-TS2),2 -> (TS3-TS4), 3->(TS5-TS6)), zone-> categorical(1,2 or 3) . 当我将扁平化的 json 提供给 pandas 中的数据框时,我想自动分配这些标签。这可能吗? (我不希望 pandas 中 get_dummies 中的 zone_1、type_1、vehicle_type_3 指标列)。如果 pandas 无法实现,请为此自动化建议 python lib。

【问题讨论】:

  • 向我们展示你的 json,以及你想要的结果。

标签: python pandas machine-learning scikit-learn sklearn-pandas


【解决方案1】:

这是我能想到的。我不知道你在寻找什么时间范围

import datetime
import io
import pandas as pd
import numpy as np
df_string='[{"id":100,"vehicle_type":"Car","time":"2017-04-06 01:39:43","zone":"A","type":"Checked"},{"id":101,"vehicle_type":"Truck","time":"2017-04-06 02:35:45","zone":"B","type":"Unchecked"},{"id":102,"vehicle_type":"Truck","time":"2017-04-05 03:20:12","zone":"A","type":"Checked"},{"id":103,"vehicle_type":"Car","time":"2017-04-04 10:05:04","zone":"C","type":"Unchecked"}]'
df = pd.read_json(io.StringIO(df_string))
df['zone'] = pd.Categorical(df.zone)
df['vehicle_type'] = pd.Categorical(df.vehicle_type)
df['type'] = pd.Categorical(df.type)
df['zone_int'] = df.zone.cat.codes
df['vehicle_type_int'] = df.vehicle_type.cat.codes
df['type_int'] = df.type.cat.codes
df.head()

编辑 这是我能想到的

import datetime
import io
import math
import pandas as pd
#Taken from http://stackoverflow.com/questions/13071384/python-ceil-a-datetime-to-next-quarter-of-an-hour
def ceil_dt(dt, num_seconds=900):
    nsecs = dt.minute*60 + dt.second + dt.microsecond*1e-6  
    delta = math.ceil(nsecs / num_seconds) * num_seconds - nsecs
    return dt + datetime.timedelta(seconds=delta)

df_string='[{"id":100,"vehicle_type":"Car","time":"2017-04-06 01:39:43","zone":"A","type":"Checked"},{"id":101,"vehicle_type":"Truck","time":"2017-04-06 02:35:45","zone":"B","type":"Unchecked"},{"id":102,"vehicle_type":"Truck","time":"2017-04-05 03:20:12","zone":"A","type":"Checked"},{"id":103,"vehicle_type":"Car","time":"2017-04-04 10:05:04","zone":"C","type":"Unchecked"}]'
df = pd.read_json(io.StringIO(df_string))
df['zone'] = pd.Categorical(df.zone)
df['vehicle_type'] = pd.Categorical(df.vehicle_type)
df['type'] = pd.Categorical(df.type)
df['zone_int'] = df.zone.cat.codes
df['vehicle_type_int'] = df.vehicle_type.cat.codes
df['type_int'] = df.type.cat.codes
df['time'] = pd.to_datetime(df.time)
df['dayofweek'] = df.time.dt.dayofweek
df['month_int'] = df.time.dt.month
df['year_int'] = df.time.dt.year
df['day'] = df.time.dt.day
df['date'] = df.time.apply(lambda x: x.date())
df['month'] = df.date.apply(lambda x: datetime.date(x.year, x.month, 1))
df['year'] = df.date.apply(lambda x: datetime.date(x.year, 1, 1))
df['hour'] = df.time.dt.hour
df['mins']  = df.time.dt.minute
df['seconds'] = df.time.dt.second
df['time_interval_3hour'] = df.hour.apply(lambda x : math.floor(x/3)+1)
df['time_interval_6hour'] = df.hour.apply(lambda x : math.floor(x/6)+1)
df['time_interval_12hour'] = df.hour.apply(lambda x : math.floor(x/12)+1)
df['weekend']  = df.dayofweek.apply(lambda x:  x>4)

df['ceil_quarter_an_hour'] =df.time.apply(lambda x : ceil_dt(x))
df['ceil_half_an_hour'] =df.time.apply(lambda x : ceil_dt(x, num_seconds=1800))
df.head()

【讨论】:

  • 我正在寻找像同一小时、同一天这样的范围,然后将它们分组为一个类别。基本上也是一种根据范围进行分类的方法 - 时间、数字。
  • 谢谢。完美。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-18
  • 2016-11-03
  • 2020-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多