Untitled
unknown
python
2 years ago
2.1 kB
6
Indexable
# Function to get average tone for a given day and country def get_average_tone_for_day(gd2, date, country_code): results = gd2.Search([date], table='events', coverage=True) condition = (results['Actor1CountryCode'] == country_code) | (results['Actor2CountryCode'] == country_code) average_tone = results[condition]['AvgTone'].mean() return date, country_code, average_tone # Create a list of dates you want to analyze start_date = pd.to_datetime('2023-10-02') end_date = pd.to_datetime('2023-10-02') date_range = pd.date_range(start_date, end_date) # Specify the country codes you are interested in country_codes = ['USA', 'FRA', 'CHN', 'IND', 'FIN', 'MEX', 'ARG', 'SWE', 'NOR', 'ISL', 'BLR', 'ESP', 'BEL', 'EST', 'CHE', 'LVA', 'SRB','DEU', 'NZL', 'PRT', 'HRV', 'NPL', 'ZAF', 'SEN', 'AUS', 'CAN', 'NLD', 'SVK', 'CRI', 'POL', 'MLT', 'ECU','LTU','AUT', 'TTO', 'GBR', 'PHL', 'IRL', 'CYP', 'ITA', 'LUX', 'ZWE', 'BWA', 'DNK', 'UGA', 'CYP', 'RUS', 'AZE', 'CHL', 'KEN', 'GHA', 'COL', 'CUB', 'TZA', 'PER', 'ZMB', 'BRA', 'CMR', 'PAN', 'NIC', 'URY', 'LKA', 'BOL', 'BFA', 'ETH', 'JPN', 'MNG', 'ISR', 'GTM', 'HUN', 'MAR', 'LAO', 'BGR', 'BGD', 'BHR', 'MYS', 'GAB', 'NGA', 'TUN', 'VNM', 'PNG', 'SLV', 'NER', 'COG', 'IDN', 'IRQ', 'IRN', 'TUR', 'THA', 'JOR', 'PAK', 'LBN', 'MMR', 'MLI', 'QAT', 'OMN', 'EGY', 'GIN', 'KHM', 'BEN','CIV'] # Version 2 queries gd2 = gdelt.gdelt(version=2) # List to store results results_list = [] # Time delay between iterations (in seconds) time_delay = 1 # Loop through dates and countries for date in date_range: for country_code in country_codes: result = get_average_tone_for_day(gd2, date.strftime('%Y%m%d'), country_code) results_list.append(result) time.sleep(time_delay) # Introduce a time delay # Create a DataFrame from the results columns = ['Date', 'CountryCode', 'AverageTone'] df_results = pd.DataFrame(results_list, columns=columns) # Print or further analyze the resulting DataFrame print(df_results)
Editor is loading...
Leave a Comment