简体   繁体   English

如何使用字典格式的重复值从 pandas dataframe 创建 aa 列

[英]How to create a a column from a pandas dataframe with the repeated values in dictionary format

i'm very confused on how to do this, (i'm very newbie yet) and I need to convert this dataframe into a dictionary with a column for repeated values:我对如何执行此操作感到很困惑(我还是新手),我需要将此 dataframe 转换为包含重复值列的字典:

import pandas as pd
df = pd.DataFrame({'Name': [['John', 'hock'], ['John','pepe'],['Peter', 'wdw'],['Peter'],['John'], ['Stef'], ['John']],
                   'Age': [38, 47, 63, 28, 33, 45, 66]
                  })

and i need something like:我需要这样的东西:

Name Age Repeated:
John 38  4

thanks!谢谢!

Use DataFrame.explode with GroupBy.size :使用DataFrame.explodeGroupBy.size

df = df.explode('Name').groupby(['Name']).size().reset_index(name='Repeated')
print (df)
    Name  Repeated
0   John         4
1  Peter         2
2   Stef         1
3   hock         1
4   pepe         1
5    wdw         1

I can think of something like:我可以想到类似的东西:

resultDict = {}
for index, row in df.iterrows():
  for value in row["Name"]:
    if value not in resultDict:
      resultDict[value] = 0
    resultDict[value] += 1
resultDict

Output Output

{'John': 4, 'Peter': 2, 'Stef': 1, 'hock': 1, 'pepe': 1, 'wdw': 1}

If you want to have it as a dataframe and not a dictionary:如果您想将其作为 dataframe 而不是字典:

resultDict = {}
for index, row in df.iterrows():
  for value in row["Name"]:
    if value not in resultDict:
      resultDict[value] = 0
    resultDict[value] += 1
pd.DataFrame({"Name":resultDict.keys(), "Repeated":resultDict.values()})

Output Output

Name名称 Repeated重复
John约翰 4 4个
hock飞节 1 1个
pepe佩佩 1 1个
Peter彼得 2 2个
wdw wdw 1 1个
Stef斯特夫 1 1个

暂无
暂无

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

相关问题 根据 Pandas 数据框中的列值创建嵌套字典? - Create nested dictionary based on column values from Pandas dataframe? Pandas:使用字典中元组的列标题和单元格值创建 dataframe - Pandas: create dataframe with column headers and cell values from tuples in a dictionary 如何从字典中创建一个pandas数据框,列名作为键,值作为行,其中值是二维数组 - how to create a pandas dataframe from a dictionary with column names as keys and values as rows where the values are 2-d array 在 pandas 中的 dataframe 中创建列的唯一值字典 - Create a dictionary of unique values of a column in a dataframe in pandas 如何从具有特定格式的 Pandas 数据框创建字典 - How do I create a dictionary from a Pandas dataframe with a specific format 如何用字典中的查找值替换 pandas DataFrame 列? - How to replace a pandas DataFrame column with lookup values from a dictionary? 如何从具有多个值的 pandas dataframe 创建字典 - How to create a dictionary from a pandas dataframe with multiple values 如何在pandas数据框中提取字典列的值 - How extract values of dictionary column in pandas dataframe 如何为列表中的一个键创建具有多个值的 Python 字典,然后创建具有一列和多行的 pandas 数据框 - How can I create a Python dictionary with multiple values for one key from a list, to then create a pandas dataframe with one column and multiple rows 熊猫:如何检查一列中的重复值并从另一列创建成对的值列表 - pandas: how to check for repeated values in one column and create a pairwise list of values from another column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM