简体   繁体   English

Python - 如何从包含字典的字典的列中计算每个唯一键的频率?

[英]Python - How to count the frequency of each unique key from a column containing a dictionary of dictionaries?

I have a very large dataframe containing a column called 'time_words'.我有一个非常大的 dataframe,其中包含一个名为“time_words”的列。 Each cell of the column contains a list of dictionaries, for example:该列的每个单元格都包含一个字典列表,例如:

time_columns时间列
{' Yesterday ': {'text': 'Yesterday', 'type': 'DATE', 'value': '2022-04-15'}} {'昨天':{'text':'昨天','type':'DATE','value':'2022-04-15'}}
{' Yesterday ': {'text': 'Yesterday', 'type': 'DATE', 'value': '2022-04-16'}, 'Thursday': {'text': 'Thursday', 'type': 'DATE', 'value': '2022-04-14'}} {'昨天':{'text':'昨天','type':'DATE','value':'2022-04-16'},'星期四':{'text':'星期四','type ':'日期','价值':'2022-04-14'}}

How can I efficiently get a table containing the frequency count of the unique keys of the main dictionary like below?我怎样才能有效地得到一个包含主字典唯一键频率计数的表,如下所示? (In a table because I want to save the result to a CSV.) (在一个表中,因为我想将结果保存到一个 CSV。)

text文本 count数数
Yesterday昨天 2 2个
Thursday周四 1 1个

Try:尝试:

df = (
    df["time_columns"]
    .explode()
    .value_counts()
    .reset_index(name="count")
    .rename(columns={"index": "text"})
)
print(df)

Prints:印刷:

        text  count
0  Yesterday      2
1   Thursday      1

Given the input data, could you try this?给定输入数据,你能试试这个吗?

tmp=pd.concat(([pd.DataFrame.from_dict(v,orient='index') for k,v in df['time_columns'].items()]))
tmp['text'].value_counts()

The easy way would be to just iterate through list and save results to new dictionary sth like:简单的方法是遍历列表并将结果保存到新字典中,例如:

res = {}
for dict in df['time_columns']:
    for key in dict.keys():
        if key not in res.keys():
             res[key] = 1
        else:
             res[key] += 1

If you know keys in advance you can initialize dict with keys and zeros and replace if statement inside the loop with just increment.如果你提前知道键,你可以用键和零初始化字典,并用增量替换循环内的 if 语句。

keys = ['Yesterday', 'Thursday', 'etc.']
res = {key: 0 for key in keys}
for dict in df['time_columns']:
    for key in dict.keys():
        res[key] += 1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:如何遍历词典列表并将每个词典作为值添加到具有唯一键的新词典中-无需重复 - Python: How to iterate over a list of dictionaries and add each dictionary as value to a new dictionary with a unique key - no repeats 如何计算包含列表的字典中每个键的每个唯一值? - How do I count each unique value for each key in a dictionary containing lists? 创建包含Excel列中每个唯一项计数的字典 - Create dictionary with count of each unique item from an excel column 如何将列的唯一值和 append 每个值计算到字典中? - How to count the unique values of a column and append each value to a dictionary? 如何遍历包含字典的列表并检查Python中每个字典中键的值? - How do I iterate over a list containing dictionaries and check the values of the key in each of the dictionaries in Python? 如何在Python中使用for循环从字符串中打印每个唯一单词的频率 - How to print frequency of each unique word from a string with for loop in python Pyspark 数据框列包含字典数组,希望将字典中的每个键变成一列 - Pyspark dataframe column contains array of dictionaries, want to make each key from dictionary into a column 将一列字典列表转换为一个列列表,以便从列表中每个字典下的键“name”派生值 - Convert a column of list of dictionaries to a column list such that the values are derived from the key "name" under each dictionary in the list 如何创建密钥字典:column_name和value:python中来自数据框的列中的唯一值 - How to create a dictionary of key : column_name and value : unique values in column in python from a dataframe 在Python中,计算字典中的唯一键/值对 - In Python, count unique key/value pairs in a dictionary
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM