【问题标题】:Use the column of a dataframe that has a list of dictionaries to create other columns for the dataframe使用具有字典列表的数据框的列为数据框创建其他列
【发布时间】:2021-01-28 14:02:03
【问题描述】:

我的数据框中有一列对象类型,其值如下:

for i in df3['placeholders'][:10]:

Output:
[{'type': 'experience', 'label': '0-1 Yrs'}, {'type': 'salary', 'label': '1,00,000 - 1,25,000 PA.'}, {'type': 'location', 'label': 'Chennai'}]
[{'type': 'date', 'label': '08 October - 13 October'}, {'type': 'salary', 'label': 'Not disclosed'}, {'type': 'location', 'label': 'Chennai'}]
[{'type': 'education', 'label': 'B.Com'}, {'type': 'salary', 'label': 'Not disclosed'}, {'type': 'location', 'label': 'Mumbai Suburbs, Navi Mumbai, Mumbai'}]
[{'type': 'experience', 'label': '0-2 Yrs'}, {'type': 'salary', 'label': '50,000 - 2,00,000 PA.'}, {'type': 'location', 'label': 'Chennai'}]
[{'type': 'experience', 'label': '0-1 Yrs'}, {'type': 'salary', 'label': '2,00,000 - 2,25,000 PA.'}, {'type': 'location', 'label': 'Bengaluru(JP Nagar)'}]
[{'type': 'experience', 'label': '0-3 Yrs'}, {'type': 'salary', 'label': '80,000 - 2,00,000 PA.'}, {'type': 'location', 'label': 'Hyderabad'}]
[{'type': 'experience', 'label': '0-5 Yrs'}, {'type': 'salary', 'label': 'Not disclosed'}, {'type': 'location', 'label': 'Hyderabad'}]
[{'type': 'experience', 'label': '0-1 Yrs'}, {'type': 'salary', 'label': '1,25,000 - 2,00,000 PA.'}, {'type': 'location', 'label': 'Mumbai'}]
[{'type': 'date', 'label': '08 October - 17 October'}, {'type': 'salary', 'label': 'Not disclosed'}, {'type': 'location', 'label': 'Pune(Bavdhan)'}]
[{'type': 'experience', 'label': '0-2 Yrs'}, {'type': 'salary', 'label': 'Not disclosed'}, {'type': 'location', 'label': 'Jaipur'}]
[{'type': 'experience', 'label': '0-0 Yrs'}, {'type': 'salary', 'label': '1,00,000 - 1,50,000 PA.'}, {'type': 'location', 'label': 'Delhi NCR(Sector-81 Noida)'}]

我想通过从该列中提取特征来向我现有的数据框添加更多列

“类型”的值= 列名

“标签”的值= 列下的值

最终预期输出:

df.head(3)

Output:

..... experience, salary, location, date, education

..... 0-1 Yrs, 1,00,000 - 1,25,000 PA., Chennai, nan, nan
..... nan, 1,00,000 - 1,25,000 PA., Chennai, 08 October - 13 October, nan
..... nan, Not disclosed, Mumbai Suburbs, Navi Mumbai, Mumbai, nan, B.Com

第一个答案有效。 [编辑 2]

后来,我尝试了第一个响应中建议的相同代码,用于具有相同问题的新数据集。我收到以下错误:

<ipython-input-23-ad8e644044af> in <listcomp>(.0)
----> 1 new_columns = set([d['Name'] for l in dfr.RatingDistribution.values for d in l ])
      2 # Make a dict of dicts
      3 col_val_dict = {}
      4 for col_name in new_columns:
      5     col_val_dict[col_name] = {}

TypeError: 'float' object is not iterable

我的输入栏:

RatingDistribution
[{'Name': 'Work-Life Balance', 'count': 5}, {'Name': 'Skill Development', 'count': 5}, {'Name': 'Salary & Benefits', 'count': 5}, {'Name': 'Job Security', 'count': 5}, {'Name': 'Company Culture', 'count': 5}, {'Name': 'Career Growth', 'count': 5}, {'Name': 'Work Satisfaction', 'count': 5}]
[{'Name': 'Work-Life Balance', 'count': 4}, {'Name': 'Skill Development', 'count': 5}, {'Name': 'Salary & Benefits', 'count': 4}, {'Name': 'Job Security', 'count': 4}, {'Name': 'Company Culture', 'count': 3}, {'Name': 'Career Growth', 'count': 3}, {'Name': 'Work Satisfaction', 'count': 5}]
[{'Name': 'Work-Life Balance', 'count': 3}, {'Name': 'Skill Development', 'count': 4}, {'Name': 'Salary & Benefits', 'count': 5}, {'Name': 'Job Security', 'count': 4}, {'Name': 'Company Culture', 'count': 5}, {'Name': 'Career Growth', 'count': 4}, {'Name': 'Work Satisfaction', 'count': 4}]
[{'Name': 'Work-Life Balance', 'count': 5}, {'Name': 'Skill Development', 'count': 5}, {'Name': 'Salary & Benefits', 'count': 5}, {'Name': 'Job Security', 'count': 5}, {'Name': 'Company Culture', 'count': 5}, {'Name': 'Career Growth', 'count': 5}, {'Name': 'Work Satisfaction', 'count': 5}]
[{'Name': 'Work-Life Balance', 'count': 3}, {'Name': 'Skill Development', 'count': 5}, {'Name': 'Salary & Benefits', 'count': 3}, {'Name': 'Job Security', 'count': 3}, {'Name': 'Company Culture', 'count': 3}, {'Name': 'Career Growth', 'count': 3}, {'Name': 'Work Satisfaction', 'count': 4}]
[{'Name': 'Work-Life Balance', 'count': 3}, {'Name': 'Skill Development', 'count': 5}, {'Name': 'Salary & Benefits', 'count': 5}, {'Name': 'Job Security', 'count': 1}, {'Name': 'Company Culture', 'count': 3}, {'Name': 'Career Growth', 'count': 1}, {'Name': 'Work Satisfaction', 'count': 1}]

我的代码:

new_columns = set([d['Name'] for l in dfr.RatingDistribution.values for d in l ])
# Make a dict of dicts 
col_val_dict = {}
for col_name in new_columns:
    col_val_dict[col_name] = {}
    # For each column name look to see if a row has that as a type
    # If so, get the label for that dict
    # otherwise fill it with NaN
    for i,l in enumerate(dfr.placeholders.values):
        the_label = [d['count'] for d in l if d['Name'] == col_name]
        if the_label:
            col_val_dict[col_name][i] = the_label[0]
        else:
            col_val_dict[col_name][i] = np.NaN
            
# Merge this new dfa with the old one
merged_dfa = pd.concat([dfr,pd.DataFrame(col_val_dict)],axis='columns')
dfr.shape

我在第一行遇到错误。我无法弄清楚为什么它会抛出浮动错误。

请帮忙

【问题讨论】:

  • 有什么解决办法吗?我很想解决这个问题:)

标签: python python-3.x pandas dataframe feature-extraction


【解决方案1】:
# Get the unique types (column names)
new_columns = set([d['type'] for l in df3.placeholders.values for d in l ])
# Make a dict of dicts 
col_val_dict = {}
for col_name in new_columns:
    col_val_dict[col_name] = {}
    # For each column name look to see if a row has that as a type
    # If so, get the label for that dict
    # otherwise fill it with NaN
    for i,l in enumerate(df3.placeholders.values):
        the_label = [d['label'] for d in l if d['type'] == col_name]
        if the_label:
            col_val_dict[col_name][i] = the_label[0]
        else:
            col_val_dict[col_name][i] = np.NaN
            
# Merge this new df with the old one
merged_df = pd.concat([df3,pd.DataFrame(col_val_dict)],axis='columns')

【讨论】:

  • TypeError ----> 1 new_columns = set([d['type'] for l in df.placeholders.values for d in l ]) 2 # 制作一个字典 3 col_val_dict = {} 4 for col_name in new_columns: 5 col_val_dict[col_name] = {} TypeError: string indices must be integers I got this error
  • from ast import literal_eval df3['placeholders']=df3['placeholders'].apply(literal_eval) 这有帮助!
  • 嗨,我用另一个数据集尝试了相同的代码。我得到了这个错误。 in (.0) ----> 1 new_columns = set([d['Name'] for l in dfr.RatingDistribution.values for d in l ]) 2 #为 new_columns 中的 col_name 制作 dicts 3 col_val_dict = {} 4 的字典:5 col_val_dict[col_name] = {} TypeError: 'float' object is not iterable 我已经用新测试的输入和输出更新了查询。请帮我解决问题。
  • @sachinkumars 使用您提供的数据,我可以毫无问题地创建列表new_columns,因此我无法重新创建错误。我的故障排除建议是看看当您尝试逐步运行列表理解时会得到什么。 ([l for l in dfr.RatingDistribution.values] 应该给你一个包含你的字典值的列表)。然后尝试在您的字典中提取与 name 键关联的值。使用您提供的数据,我不确定您在哪里迭代浮点索引,但这样做应该可以识别它。
猜你喜欢
  • 1970-01-01
  • 2021-08-18
  • 2023-02-05
  • 1970-01-01
  • 2017-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多