Конвертировать CSV в JSON.Как мне сохранить значения с тем же индексом? - PullRequest
3 голосов
/ 17 мая 2019

Я использую эту базу данных: https://cpj.org/data/killed/?status=Killed&motiveConfirmed%5B%5D=Confirmed&type%5B%5D=Journalist&localOrForeign%5B%5D=Foreign&start_year=1992&end_year=2019&group_by=year

Я предварительно обработал ее в этот CSV (показывает только 2 строки из 159):

year,combinedStatus,fullName,sortName,primaryNationality,secondaryNationality,tertiaryNationality,gender,photoUrl,photoCredit,type,lastStatus,typeOfDeath,status,employedAs,organizations,jobs,coverage,mediums,country,location,region,state,locality,province,localOrForeign,sourcesOfFire,motiveConfirmed,accountabilityCrossfire,accountabilityAssignment,impunityMurder,tortured,captive,threatened,charges,motive,lengthOfSentence,healthProblems,impCountry,entry,sentenceDate,sentence,locationImprisoned
1994,Confirmed,Abdelkader Hireche,,,,,Male,,,Journalist,,Murder,Killed,Staff,Algerian Television (ENTV),Broadcast Reporter,Politics,Television,Algeria,Algiers,,,Algiers,,Foreign,,Confirmed,,,Partial Impunity,No,No,No,,,,,,,,,
2014,Confirmed,Ahmed Hasan Ahmed,,,,,Male,,,Journalist,,Dangerous Assignment,Killed,Staff,Xinhua News Agency,"Camera Operator,Photographer","Human Rights,Politics,War",Internet,Syria,Damascus,,,Damascus,,Foreign,,Confirmed,,,,,,,,,,,,,,,

И я хочу сделать этот типJSON из этого:

"Afghanistan": {"year": 2001, "fullName": "Volker Handloik", "gender": "Male", "typeOfDeath": "Crossfire", "employedAs": "Freelance", "organizations": "freelance reporter", "jobs": "Print Reporter", "coverage": "War", "mediums": "Print", "photoUrl": NaN}, "Somalia": {"year": 1994, "fullName": "Pierre Anceaux", "gender": "Male", "typeOfDeath": "Murder", "employedAs": "Freelance", "organizations": "freelance", "jobs": "Broadcast Reporter", "coverage": "Human Rights", "mediums": "Television", "photoUrl": NaN}

Проблема в том, что в Афганистане (как вы можете видеть по ссылке) было много смертей журналистов.Я хочу перечислить все эти убийства в Индексе «Афганистан».Однако, как я сейчас делаю, обнаруживается только последний случай (Volker Handloik) в файле csv.Как я могу получить его, чтобы каждый случай отображался?

это мой код atm

import pandas as pd
import pprint as pp
import json

# list with stand-ins for empty cells
missing_values = ["n/a", "na", "unknown", "-", ""]

# set missing values to NaN
df = pd.read_csv("data_journalists.csv", na_values = missing_values, skipinitialspace = True, error_bad_lines=False)

# columns
columns_keep = ['year', 'fullName', 'gender', 'typeOfDeath', 'employedAs', 'organizations', 'jobs', 'coverage', 'mediums', 'country', 'photoUrl']

small_df = df[columns_keep]

with pd.option_context('display.max_rows', None, 'display.max_columns', None):  # more options can be specified also
    print(small_df)

# create dict with country-column as index
df_dict = small_df.set_index('country').T.to_dict('dict')

print(df_dict)

# make json file from the dict
with open('result.json', 'w') as fp:
    json.dump(df_dict, fp)

    # use pretty print to see if dict matches the json example in the exercise
 pp.pprint(df_dict)

Я хочу включить все эти имена (и более) в JSON под индексом Афганистан

enter image description here

Я думаю, что мне понадобится список объектов, который прикреплен к индексу страны, чтобы каждая страна могла показать все случаи журналистовсмерть вместо только 1 (каждый раз заменяется следующим в CSV) Я надеюсь, что это достаточно ясно

1 Ответ

1 голос
/ 17 мая 2019

Я буду хранить ваш код до определения small_df.

После этого мы выполняем групповую операцию в столбце 'страна' и используем для него pd.to_json:

country_series = small_df.groupby('country').apply(lambda r : r.drop(['country'], axis=1).to_json())

country_series - это pd.Series со странами в качестве индекса. После этого мы создаем вложенный словарь, чтобы у нас был действительный объект json:

fullDict = {}
for ind, a in country_series.iteritems():
    b = json.loads(a)
    c = b['fullName']
    smallDict = {}
    for index, journalist in c.items():
        smallDict[journalist] = {}
        for i in b.keys():
            smallDict[journalist][i] = b[i][index]
    fullDict[ind] = (smallDict)

Номенклатура в моей части кода довольно плохая, но я попытался написать все шаги явно, чтобы все было понятно.

Наконец, мы записываем результаты в файл:

with open('result.json', 'w') as f:
    json.dump(fullDict, f)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...