0

I am having troubles to make follwing "print" into CSV file. How can I put the output to CSV?

for index, row in df.head(5000).iterrows():
    if row["year"] != Year:
        print(f"{Year} has new actors {New_actors}")
        print("---------------------------------------------")
        New_actors.clear()
        Year = row["year"]

    if row["country"] not in New_actors:
        New_actors[row["country"]] = list()
        New_actors[row["country"]]

    if row["country"] not in Known_actors:
        New_actors[row["country"]].append(row["actor1"])
        New_actors[row["country"]].append(row["actor2"])
        Known_actors[row["country"]] = set()

    else:
        if row["actor1"] not in Known_actors[row["country"]]:
            New_actors[row["country"]].append(row["actor1"])
        if row["actor2"] not in Known_actors[row["country"]]:
            New_actors[row["country"]].append(row["actor2"])

    Known_actors[row["country"]].add(row["actor1"])
    Known_actors[row["country"]].add(row["actor2"])

print(f"{Year} has new actors {New_actors}")

I get the result like the following which I want to output to csv file. I assume I need to make the result into dataframe first, then output to CSV, but having troubles to do so.

================================ 1997 has new actors {'Angola': ['UNITA: National Union for the Total Independence of Angola', 'Civilians (Angola)', 'Military Forces of Angola (1975-)', 'Protesters (Angola)', nan, 'Former UNITA: National Union for the Total Independence of Angola', 'FLEC-FAC: Front for the Liberation of the Enclave of Cabinda-Armed Forces of Cabinda', 'Unidentified Armed Group (Angola)', 'Civilians (South Africa)', 'Civilians (International)', 'Military Forces of Republic of Congo (1992-1997)', 'FLEC-Renouvada: Front for the Liberation of the Enclave of Cabinda (Renouvada Faction)', 'FLEC: Front for the Liberation of the Enclave of Cabinda', 'Rioters (Angola)', 'FDC: Democratic Front of Cabinda', 'Military Forces of Democratic Republic of Congo (1997-2001)'],

Many thanks,

0 Answers0