我在pandas数据框中有数据,如下所示:
queryName Market tags categoryDetails
dummy_query (dummy_market) dummy_market dummy_tag [{'name': 'relevant_data', 'parentName': 'relevant_scrape', 'parentId': '289245228', 'id': '2892695401'}, {'name': 'relevant_data', 'parentName': 'relevant_scrape', 'parentId': '289245228', 'id': '21892718'}, {'name': 'dummy_data', 'parentName': 'Location', 'parentId': '21221517840', 'id': '229565351'}]
dummy_query (dummy_market) dummy_market dummy_tag [{'name': 'relevant_data', 'parentName': 'relevant_scrape', 'parentId': '289245228', 'id': '2892659'}, {'name': 'relevant_data', 'parentName': 'relevant_scrape', 'parentId': '289245228', 'id': '2892667'}, {'name': 'irrelevant_data', 'parentName': 'irrelevant_scrape', 'parentId': '2662610', 'id': '268415777'}, {'name': 'dummy_data', 'parentName': 'Location', 'parentId': '21221517840', 'id': '2565351'}]
dummy_query (dummy_market) dummy_market dummy_tag [{'name': 'relevant_data', 'parentName': 'relevant_scrape', 'parentId': '289245228', 'id': '2892695401'}, {'name': 'irrelevant_data', 'parentName': 'irrelevant_scrape', 'parentId': '2662610', 'id': '268415777'}, {'name': 'dummy_data', 'parentName': 'Location', 'parentId': '21221517840', 'id': '229565351'}, {'name': 'Consideration', 'parentName': 'irrelevant_scrape', 'parentId': '2203873', 'id': '2203874'}]
dummy_query (dummy_market) dummy_market dummy_tag [{'name': 'relevant_data', 'parentName': 'relevant_scrape', 'parentId': '289245228', 'id': '2892695401'}, {'name': 'irrelevant_data', 'parentName': 'irrelevant_scrape', 'parentId': '2662610', 'id': '268415777'}, {'name': 'dummy_data', 'parentName': 'Location', 'parentId': '21221517840', 'id': '229565351'}]
dummy_query (dummy_market) dummy_market dummy_tag [{'name': 'relevant_data', 'parentName': 'relevant_scrape', 'parentId': '289245228', 'id': '21892718'}, {'name': 'irrelevant_data', 'parentName': 'irrelevant_scrape', 'parentId': '2662610', 'id': '268415777'}, {'name': 'dummy_data', 'parentName': 'Location', 'parentId': '21221517840', 'id': '229565351'}]
dummy_query (dummy_market) dummy_market dummy_tag [{'name': 'relevant_data', 'parentName': 'relevant_scrape', 'parentId': '289245228', 'id': '2892659'}, {'name': 'dummy_data', 'parentName': 'Location', 'parentId': '21221517840', 'id': '229565351'}, {'name': 'dummy_data', 'parentName': 'irrelevant_scrape', 'parentId': '2203873', 'id': '2203880'}]
我需要我的数据框有一个额外的第五列,它将包含所有名称键,每行包含名为“relevant_data”的元素。这些数据点是根据parentName选择的。如果parentName ='relevant_scrape',请选择“name”。
我应该怎么做呢?到目前为止,这是我的代码。
import pandas as pd
import json
from pandas import DataFrame, read_csv
df = pd.read_csv('dataset.csv', sep = '\t')
for row in df.categoryDetails:
if isinstance(row, str):
list_dicts = json.loads(row.replace("'", "\""))
for each_dict in list_dicts:
if each_dict["parentName"] == "relevant_scrape":
df['fifth_column'] = each_dict["name"]
df.to_csv('output.txt', sep = '\t')
(注意:我的原始数据有点混乱,在用双引号替换它的引号之前无法呈现为JSON。因此json.loads调用。)
这为我生成了一个带有第五列的数据框,但它在每一行中插入了相同的“name”元素。感谢所有的帮助,谢谢。
答案 0 :(得分:1)
您正在使用df['fifth_column'] = each_dict["name"]
,它会将'fifth_column'
列中的所有值设置为每次迭代的相同值,因为pandas'默认情况下,操作是列式的。
也许您应该尝试以下代码段:
def extract_details(row):
# your parsing logic.
if isinstance(row, str):
list_dicts = json.loads(row.replace("'", "\""))
all_relevant_data = []
for each_dict in list_dicts:
if each_dict["parentName"] == "relevant_scrape":
all_relevant_data.append(each_dict["name"])
return ','.join(all_relevant_data)
然后你可以这样做:
df['fifth_column'] = df.categoryDetails.apply(extract_details)