[英]Pandas date_range to nested json
I want to get a nested dict from pandas date_range. 我想从pandas date_range获得一个嵌套的dict。 The keys of the dictionary should be the date and the values, a list of all values in the selected range. 字典的键应该是日期和值,是所选范围内所有值的列表。 For instance: 例如:
import numpy as np
import pandas as pd
import arrow
mymatrix = pd.DataFrame([
[pd.Timestamp(arrow.get('01-01-2017 01:01:00', 'DD-MM-YYYY HH:mm:ss').naive), 5],
[pd.Timestamp(arrow.get('01-01-2017 00:02:00', 'DD-MM-YYYY HH:mm:ss').naive), 10],
[pd.Timestamp(arrow.get('01-01-2017 00:03:00', 'DD-MM-YYYY HH:mm:ss').naive), 20],
[pd.Timestamp(arrow.get('01-01-2017 00:04:00', 'DD-MM-YYYY HH:mm:ss').naive), 30]
]) ])
and I want to get something like: 我希望得到类似的东西:
by minute: 分钟:
{
'01-01-2017 01:01:00': [5],
'01-01-2017 00:02:00': [10],
'01-01-2017 00:03:00': [20],
'01-01-2017 00:04:00': [30]
}
by hour: 按小时:
{
'01-01-2017 01': [5],
'01-01-2017 00: [10, 20, 30]
}
is for large dataset so I want to avoid loops, I would like to do this directly in pandas/numpy. 对于大型数据集,所以我想避免循环,我想直接在pandas / numpy中这样做。
can anybody help me. 有谁能够帮助我。
Thank you in advance 先感谢您
Try this: 尝试这个:
In [46]: x
Out[46]:
0 1
0 2017-01-01 01:01:00 5
1 2017-01-01 00:02:00 10
2 2017-01-01 00:03:00 20
3 2017-01-01 00:04:00 30
In [47]: x.groupby(x[0].dt.minute)[1].apply(list)
Out[47]:
0
1 [5]
2 [10]
3 [20]
4 [30]
Name: 1, dtype: object
In [48]: x.groupby(x[0].dt.hour)[1].apply(list)
Out[48]:
0
0 [10, 20, 30]
1 [5]
Name: 1, dtype: object
Not sure what arrow is but I would try and keep everything in pandas/numpy. 不知道箭头是什么,但我会尝试将所有内容保存在pandas / numpy中。
mymatrix = pd.DataFrame([
[pd.Timestamp('01-01-2017 01:01:00'), 5],
[pd.Timestamp('01-01-2017 00:02:00'), 10],
[pd.Timestamp('01-01-2017 00:03:00'), 20],
[pd.Timestamp('01-01-2017 00:04:00'), 30]])
You can then set the index and use resample
. 然后,您可以设置索引并使用resample
。
df = mymatrix.set_index(0)
1
0
2017-01-01 01:01:00 5
2017-01-01 00:02:00 10
2017-01-01 00:03:00 20
2017-01-01 00:04:00 30
df.resample('H').apply(lambda x: x.values.tolist()).to_dict()[1]
{Timestamp('2017-01-01 00:00:00'): [10, 20, 30],
Timestamp('2017-01-01 01:00:00'): [5]}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.