使用python将过滤后的数据帧存储在新数据帧中

时间:2017-05-24 07:46:43

标签: python json pandas dataframe

我的数据帧约为142264行:

样品:

              DateAndTime  TAGID  TagValue  UIB
  2017-04-26 00:00:00.000    100       0.9  NaN
  2017-04-26 00:00:00.000    101     430.3  NaN
  2017-04-26 00:00:00.000    102     112.7  NaN
  2017-04-26 00:00:00.000    103      50.0  NaN
  2017-04-26 00:00:00.000    104     249.4  NaN
  2017-04-26 00:00:00.000    105     109.9  NaN
  2017-04-26 00:00:00.000    106     248.4  NaN
  2017-04-26 00:00:00.000    107     131.5  NaN
  2017-04-26 00:00:00.000    108     247.7  NaN
  2017-04-26 00:00:00.000    109      96.8  NaN
  2017-04-26 00:00:00.000    113     481.4  NaN
  2017-04-26 00:00:00.000    114     243.9  NaN
  2017-04-26 00:00:00.000    115    -416.0  NaN
  2017-04-26 00:00:00.000    116      -0.5  NaN
  2017-04-26 00:00:00.000    117     429.2  NaN
  2017-04-26 00:00:00.000    118     646.4  NaN
  2017-04-26 00:00:00.000    119      49.5  NaN
  2017-04-26 00:00:00.000    120     248.2  NaN
  2017-04-26 00:01:00.000    100       0.9  NaN
  2017-04-26 00:01:00.000    101     429.7  NaN
  2017-04-26 00:01:00.000    102     120.0  NaN
  2017-04-26 00:01:00.000    103      49.9  NaN
  2017-04-26 00:01:00.000    104     249.2  NaN
  2017-04-26 00:01:00.000    105     123.8  NaN
  2017-04-26 00:01:00.000    106     248.3  NaN
  2017-04-26 00:01:00.000    107     136.3  NaN
  2017-04-26 00:01:00.000    108     247.4  NaN
  2017-04-26 00:01:00.000    109      99.9  NaN
  2017-04-26 00:01:00.000    113     481.4  NaN
  2017-04-26 00:01:00.000    114     243.9  NaN

我想在唯一的tagid上过滤数据帧并单独存储新的数据帧。

我试过了:

data = read_json("json_tagid_100_120.json")
tagid101 = data[data["TAGID"] == 101]
print tagid101

通过这样做,我只能存储Tagid 101的数据。

但我希望将各个tagid的数据存储在新的数据框中。

1 个答案:

答案 0 :(得分:1)

我认为最好通过将dict对象转换为DataFrames然后转换为DataFrameGroupBy来创建所有tuple dict

dfs = dict(tuple(data.groupby("TAGID")))

print (dfs[101])
           DateAndTime  TAGID  TagValue  UIB
1  2017-04-26 00:00:00    101     430.3  NaN
19 2017-04-26 00:01:00    101     429.7  NaN