![](/img/trans.png)
[英]Convert the dictionary to data frame in pyspark for unicode key and list of float values
[英]Convert Dictionary of Dictionaries with list values to a data frame
我收到了一本非常大的字典,格式如下,我不确定如何将其转换为可用于执行基本功能的 dataframe。
{
'hash': {
'ids': [List of Unique IDs of records this hash has been seen in],
'weights': [List of weights],
'values': [List of values],
'measure_dates': [List of dates]
}
}
ids
、 weights
、 values
和measure_dates
中的项目数在hash
中是相同的。 不过,不同的hash
es 可以有不同数量的项目。 这取决于进行测量的频率。
三个记录示例的真实(ish)数据:
{
'IRR-99876-UTY': {
'ids': [9912234, 9912237, 45555889],
'weights': [0.09, 0.09, 0.113],
'values': [2.31220, 2.31219, 2.73944],
'measure_dates': ['2021-10-14', '2021-10-15', '2022-12-17']
},
'IRR-10881-CKZ': {
'ids': [45557231],
'weights': [0.31],
'values': [5.221001],
'measure_dates': ['2022-12-31']
},
'IRR-881-CKZ': {
'ids': [24661, 24662, 29431],
'weights': [0.05, 0.07, 0.105],
'values': [3.254, 4.500001, 7.3221],
'measure_dates': ['2018-05-05', '2018-05-06', '2018-07-01']
}
}
索引中的值对应于进行的相同测量。 例如在IRR-881-CKZ
中,有 3 个测量值。
24661
,权重为0.05
,值为3.254
24662
,权重为0.07
,值为4.500001
29431
,权重为0.105
,值为7.3221
没有其他索引组合对此 hash 有效。
我将尝试获取数据的信息:
ids
列表中的项目数量最多来确定。 在此示例中,第一条和第三条记录包含三个项目,因此将是最靠前的结果。 我希望能够使用nlargest()
或sort_values().head()
类的东西来获取它,而不是解析每条记录并计算项目的数量。df['average'] = df[['value1', 'value2']].mean(axis=1)
事情,但是可变数量的值我不知道该怎么做。如何将这本列表词典转换为可用的 dataframe?
您可以在 pandas 中使用.from_dict()
将其转换为 dataframe。
import pandas as pd
# dictionary of dictionaries with list values
data = {
'IRR-99876-UTY': {
'ids': [9912234, 9912237, 45555889],
'weights': [0.09, 0.09, 0.113],
'values': [2.31220, 2.31219, 2.73944],
'measure_dates': ['2021-10-14', '2021-10-15', '2022-12-17']
},
'IRR-10881-CKZ': {
'ids': [45557231],
'weights': [0.31],
'values': [5.221001],
'measure_dates': ['2022-12-31']
},
'IRR-881-CKZ': {
'ids': [24661, 24662, 29431],
'weights': [0.05, 0.07, 0.105],
'values': [3.254, 4.500001, 7.3221],
'measure_dates': ['2018-05-05', '2018-05-06', '2018-07-01']
}
}
# convert to data frame
df = pd.DataFrame.from_dict(data, orient='index')
您需要将此词典的每个条目转换为它自己的 DataFrame 并将它们连接起来以有效地处理此数据:
import pandas as pd
data = {
'IRR-99876-UTY': {
'ids': [9912234, 9912237, 45555889],
'weights': [0.09, 0.09, 0.113],
'values': [2.31220, 2.31219, 2.73944],
'measure_dates': ['2021-10-14', '2021-10-15', '2022-12-17']
},
'IRR-10881-CKZ': {
'ids': [45557231],
'weights': [0.31],
'values': [5.221001],
'measure_dates': ['2022-12-31']
},
'IRR-881-CKZ': {
'ids': [24661, 24662, 29431],
'weights': [0.05, 0.07, 0.105],
'values': [3.254, 4.500001, 7.3221],
'measure_dates': ['2018-05-05', '2018-05-06', '2018-07-01']
}
}
df = pd.concat(
{k: pd.DataFrame(v) for k, v in data.items()},
names=['hash', 'obs']
)
print(df)
ids weights values measure_dates
hash obs
IRR-99876-UTY 0 9912234 0.090 2.312200 2021-10-14
1 9912237 0.090 2.312190 2021-10-15
2 45555889 0.113 2.739440 2022-12-17
IRR-10881-CKZ 0 45557231 0.310 5.221001 2022-12-31
IRR-881-CKZ 0 24661 0.050 3.254000 2018-05-05
1 24662 0.070 4.500001 2018-05-06
2 29431 0.105 7.322100 2018-07-01
现在我们的数据已经清理完毕,我们可以解决您的问题了。
Series.value_counts
操作。 然而,由于我们感兴趣的数据目前在索引中,因此我们需要先使用Index.get_level_values
将其抓取出来。Series.between
方法来检查这些平均值是否存在于两个任意值之间。# Which hash(es) are measured the most often.
df.index.get_level_values('hash').value_counts()
# IRR-99876-UTY 3
# IRR-881-CKZ 3
# IRR-10881-CKZ 1
# Name: hash, dtype: int64
# ---
# Which hashes have an average value between two values.
## Here you can see that I'm testing whether the average is between 0 and 4
print(df.groupby('hash')['values'].mean().between(0, 4))
# IRR-10881-CKZ False
# IRR-881-CKZ False
# IRR-99876-UTY True
# Name: values, dtype: bool
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.