{"id":46800,"date":"2024-05-16T00:00:00","date_gmt":"2024-05-16T07:00:00","guid":{"rendered":"https:\/\/griddb-linux-hte8hndjf8cka8ht.westus-01.azurewebsites.net\/blog\/analyzing-world-population-data-in-python\/"},"modified":"2025-11-13T12:56:59","modified_gmt":"2025-11-13T20:56:59","slug":"analyzing-world-population-data-in-python","status":"publish","type":"post","link":"https:\/\/www.griddb.net\/en\/blog\/analyzing-world-population-data-in-python\/","title":{"rendered":"Analyzing World Population Data in Python"},"content":{"rendered":"<p>In this article, you will learn how to visualize world population data in Python using GridDB as the database. You will use the GridDB Python client to connect with a GridDB database, insert world population CSV data into GridDB, retrieve this data, and plot different visualizations using Python matplotlib, pandas, and seaborn libraries.<\/p>\n<p><strong>Note:<\/strong><br \/>\nThe code for this blog can be found at <a href=\"https:\/\/github.com\/griddbnet\/Blogs\">GridDB Blogs Github repo<\/a> under the branch called <code>population_analysis<\/code>.<\/p>\n<h2>Prerequisites<\/h2>\n<p>You need to install the GridDB C Client and the GridDB Python client before you can connect your Python application to GridDB. Follow the instructions on the <a href=\"https:\/\/pypi.org\/project\/griddb-python\/\">GridDB Python Package Index (Pypi)<\/a> page to install these clients.<\/p>\n<p>The script below imports the libraries you will need to run the code in this blog.<\/p>\n<div class=\"clipboard\">\n<pre><code class=\"language-python\">import griddb_python as griddb\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nsns.set(style=\"darkgrid\")<\/code><\/pre>\n<\/div>\n<h2>Importing and Preprocessing the World Population Dataset<\/h2>\n<p>We will analyze the <a href=\"https:\/\/www.kaggle.com\/datasets\/iamsouravbanerjee\/world-population-dataset\">World Population Dataset from Kaggle<\/a> with the help of different visualizations in Python. The dataset consists of a CSV file containing world population statistics of different countries from 1970 to 2022.<\/p>\n<p>Download the CSV file and run the following script to import the dataset into a Pandas dataframe. Make sure to update the path for <code>world_population.csv<\/code> file, depending upon its location.<\/p>\n<div class=\"clipboard\">\n<pre><code class=\"language-python\">dataset = pd.read_csv(r\"\/mnt\/d\/Datasets\/world_population.csv\")\nprint(dataset.shape)\ndataset.head()<\/code><\/pre>\n<\/div>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/griddb.net\/wp-content\/uploads\/2024\/05\/img1-world-population-dataset-dataframe.png\"><img fetchpriority=\"high\" decoding=\"async\" src=\"https:\/\/griddb.net\/wp-content\/uploads\/2024\/05\/img1-world-population-dataset-dataframe.png\" alt=\"\" width=\"1650\" height=\"366\" class=\"aligncenter size-full wp-image-30096\" srcset=\"\/wp-content\/uploads\/2024\/05\/img1-world-population-dataset-dataframe.png 1650w, \/wp-content\/uploads\/2024\/05\/img1-world-population-dataset-dataframe-300x67.png 300w, \/wp-content\/uploads\/2024\/05\/img1-world-population-dataset-dataframe-1024x227.png 1024w, \/wp-content\/uploads\/2024\/05\/img1-world-population-dataset-dataframe-768x170.png 768w, \/wp-content\/uploads\/2024\/05\/img1-world-population-dataset-dataframe-1536x341.png 1536w, \/wp-content\/uploads\/2024\/05\/img1-world-population-dataset-dataframe-600x133.png 600w\" sizes=\"(max-width: 1650px) 100vw, 1650px\" \/><\/a><\/p>\n<p>The dataset consists of 234 rows and 17 columns. The column names contain some special characters, which we must remove since the GridDB container does not allow column names to contain some of the special characters.<\/p>\n<p>The script below replaces special characters in dataset columns with underscores and displays the data type of the columns.<\/p>\n<div class=\"clipboard\">\n<pre><code class=\"language-python\">dataset.columns = dataset.columns.str.replace('[^a-zA-Z0-9]', '_', regex=True)\ndataset.dtypes<\/code><\/pre>\n<\/div>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/griddb.net\/wp-content\/uploads\/2024\/05\/img2-input-dataset-column-types.png\"><img decoding=\"async\" src=\"https:\/\/griddb.net\/wp-content\/uploads\/2024\/05\/img2-input-dataset-column-types.png\" alt=\"\" width=\"438\" height=\"478\" class=\"aligncenter size-full wp-image-30097\" srcset=\"\/wp-content\/uploads\/2024\/05\/img2-input-dataset-column-types.png 438w, \/wp-content\/uploads\/2024\/05\/img2-input-dataset-column-types-275x300.png 275w\" sizes=\"(max-width: 438px) 100vw, 438px\" \/><\/a><\/p>\n<p>Verify the type of your input columns since you will need to map them to GridDB-compliant column types. For example, you will have to replace the object type with the string type before inserting the data into a GridDB container, as you will see later in the article.<\/p>\n<p>We have preprocessed the dataset. The following steps include connecting with the GridDB and inserting our CSV data.<\/p>\n<p><strong>Note:<\/strong> You can directly plot data visualizations in Python using a Pandas dataframe. For the sake of demonstration in this blog, you will first insert the CSV data into a GridDB container, fetch the inserted data into a Pandas dataframe and then plot visualizations using the data retrieved from GridDB.<\/p>\n<h2>Creating a Connection with GridDB<\/h2>\n<p>To create a connection, you must create an object of the <code>StoreFactory<\/code> class, as shown in the script below.<\/p>\n<p>Next, you must pass the GridDB host URL, the cluster name, and the user and password values to the factory object&#8217;s <code>get_store()<\/code> method. And that&#8217;s it.<\/p>\n<p>To verify the connection is established, you can use the <code>get_container()<\/code> method to get any random container. If there is a connection issue, an exception will occur.<\/p>\n<p>Run the script below. If you see the provided output, your connection is successful. If an exception occurs, verify your credentials, ensure the GridDB server is running, and check your configuration settings.<\/p>\n<div class=\"clipboard\">\n<pre><code class=\"language-python\">factory = griddb.StoreFactory.get_instance()\n\nDB_HOST = \"127.0.0.1:10001\"\nDB_CLUSTER = \"myCluster\"\nDB_USER = \"admin\"\nDB_PASS = \"admin\"\n\ntry:\n    gridstore = factory.get_store(\n        notification_member = DB_HOST,\n        cluster_name = DB_CLUSTER,\n        username = DB_USER,\n        password = DB_PASS\n    )\n\n    container1 = gridstore.get_container(\"container1\")\n    if container1 == None:\n        print(\"Container does not exist\")\n    print(\"Successfully connected to GridDB\")\n\nexcept griddb.GSException as e:\n    for i in range(e.get_error_stack_size()):\n        print(\"[\", i, \"]\")\n        print(e.get_error_code(i))\n        print(e.get_location(i))\n        print(e.get_message(i))<\/code><\/pre>\n<\/div>\n<p><strong>Output:<\/strong><\/p>\n<pre><code>Container does not exist\nSuccessfully connected to GridDB\n<\/code><\/pre>\n<h2>Storing World Population Data in a GridDB Container<\/h2>\n<p>GridDB containers expect table columns to be in a specific data format. For further information, refer to <a href=\"https:\/\/docs.griddb.net\/architecture\/data-model\/#data-type\">GridDB data types<\/a>.<\/p>\n<p>To create a container, you first have to create an object of the <code>ContainerInfo<\/code> class and pass it four parameters: container name, container columns, container type, and a boolean value, which, if set to <code>True<\/code> treats the first column as the identity column.<\/p>\n<p>The container columns must be a list of lists, each nested list containing a column name and the corresponding column type. Since our dataset does not have time-series data, we will set the container type to Collection.<\/p>\n<p>The script below defines a <code>map_pandas_dtype_to_griddb()<\/code> function that maps the Pandas dataframe columns to GridDB column types. You can modify this method as per your requirements.<\/p>\n<p>Next we create an object of the <code>ContainerInfo<\/code> class that stores our container information.<\/p>\n<div class=\"clipboard\">\n<pre><code class=\"language-python\"># see all GridDB data types: https:\/\/docs.griddb.net\/architecture\/data-model\/#data-type\n\ndef map_pandas_dtype_to_griddb(dtype):\n    if dtype == 'int64':\n        return griddb.Type.LONG\n    elif type == 'float64':\n        return griddb.Type.FLOAT\n    elif dtype == 'object':\n        return griddb.Type.STRING\n    # Add more column types if you want\n    else:\n        raise ValueError(f'Unsupported pandas type: {dtype}')\n\ncontainer_columns = []\nfor column_name, dtype in dataset.dtypes.items():\n    griddb_dtype = map_pandas_dtype_to_griddb(str(dtype))\n    container_columns.append([column_name, griddb_dtype])\n\ncontainer_info = griddb.ContainerInfo(\"PopulationStats\",\n                                      container_columns,\n                                      griddb.ContainerType.COLLECTION, True)<\/code><\/pre>\n<\/div>\n<p>Finally, to create a container object, call the <code>put_container()<\/code> method and pass it the <code>container_info<\/code> object you just created.<\/p>\n<p>Finally, to insert records into your container, iterate through the rows in the input dataframe, convert each row to a Python list, and insert the list into the container object, as the following script demonstrates.<\/p>\n<div class=\"clipboard\">\n<pre><code class=\"language-python\">try:\n    cont = grid store.put_container(container_info)\n    for index, row in dataset.iterrows():\n        cont.put(row.tolist())\n    print(\"All rows have been successfully stored in the GridDB container.\")\n\nexcept griddb.GSException as e:\n    for i in range(e.get_error_stack_size()):\n        print(\"[\", i, \"]\")\n        print(e.get_error_code(i))\n        print(e.get_location(i))\n        print(e.get_message(i))<\/code><\/pre>\n<\/div>\n<p><strong>Output:<\/strong><\/p>\n<p><code>All rows have been successfully stored in the GridDB container.<\/code><\/p>\n<p>The data has been successfully inserted into GridDB, as seen in the above message.<\/p>\n<p>Next, you will learn how to read data from a GridDB container.<\/p>\n<h2>Retrieving World Population Data From a GridDB Container<\/h2>\n<p>In this section, we will fetch the world population data from the GridDB container and analyze it in the upcoming section using different visualizations.<\/p>\n<p>The <code>get_container()<\/code> method fetches container data from GridDB. The <code>query<\/code> method of the container object allows you to execute SQL queries on this container.<\/p>\n<p>In the following script, we select all the data from the <code>PopulationStats<\/code> container using the <code>fetch()<\/code> method. Next, we retrieve all rows using the <code>fetch_rows()<\/code> method, which returns data as a Pandas dataframe.<\/p>\n<div class=\"clipboard\">\n<pre><code class=\"language-python\">population_container = gridstore.get_container(\"PopulationStats\")\nquery = population_container.query(\"select *\")\nrs = query.fetch()\npopulation_data = rs.fetch_rows()\n\nprint(population_data.shape)\npopulation_data.head()<\/code><\/pre>\n<\/div>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/griddb.net\/wp-content\/uploads\/2024\/05\/img3-data-retrieved-from-a-griddb-container.png\"><img decoding=\"async\" src=\"https:\/\/griddb.net\/wp-content\/uploads\/2024\/05\/img3-data-retrieved-from-a-griddb-container.png\" alt=\"\" width=\"1718\" height=\"417\" class=\"aligncenter size-full wp-image-30098\" srcset=\"\/wp-content\/uploads\/2024\/05\/img3-data-retrieved-from-a-griddb-container.png 1718w, \/wp-content\/uploads\/2024\/05\/img3-data-retrieved-from-a-griddb-container-300x73.png 300w, \/wp-content\/uploads\/2024\/05\/img3-data-retrieved-from-a-griddb-container-1024x249.png 1024w, \/wp-content\/uploads\/2024\/05\/img3-data-retrieved-from-a-griddb-container-768x186.png 768w, \/wp-content\/uploads\/2024\/05\/img3-data-retrieved-from-a-griddb-container-1536x373.png 1536w, \/wp-content\/uploads\/2024\/05\/img3-data-retrieved-from-a-griddb-container-600x146.png 600w\" sizes=\"(max-width: 1718px) 100vw, 1718px\" \/><\/a><\/p>\n<h2>Visualizing World Population Data in Python using Data from GridDB<\/h2>\n<p>We have successfully retrieved the data we inserted into GridDB as a Pandas dataframe. Next, you can use this dataframe to analyze different aspects of the world population. You can plot different visualizations using any Python library, e.g., Pandas, Matplotlib, Seaborn, etc.<\/p>\n<p>Let&#8217;s see some examples.<\/p>\n<h3>Analyzing World Population by Continent<\/h3>\n<p>You probably already know that Asia is the most populous continent in the world. But do you know what percentage of the global population Asia constitutes? You can visualize this information using a pie chart that sums the population of all countries by continent and then displays the percentage of the population for each continent.<\/p>\n<p>The script below plots a pie chart of the world population grouped by continents.<\/p>\n<div class=\"clipboard\">\n<pre><code class=\"language-python\">population_by_continent = population_data.groupby('Continent')['2022_Population'].sum()\n\nplt.figure(figsize=(6,6))\nplt.pie(population_by_continent,\n        labels=population_by_continent.index,\n        autopct='%1.1f%%', startangle=140)\nplt.title('World Population by Continent in 2022')\nplt.show()<\/code><\/pre>\n<\/div>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/griddb.net\/wp-content\/uploads\/2024\/05\/img4-pie-chart-of-world-population-by-continent.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/griddb.net\/wp-content\/uploads\/2024\/05\/img4-pie-chart-of-world-population-by-continent.png\" alt=\"\" width=\"633\" height=\"567\" class=\"aligncenter size-full wp-image-30099\" srcset=\"\/wp-content\/uploads\/2024\/05\/img4-pie-chart-of-world-population-by-continent.png 633w, \/wp-content\/uploads\/2024\/05\/img4-pie-chart-of-world-population-by-continent-300x269.png 300w, \/wp-content\/uploads\/2024\/05\/img4-pie-chart-of-world-population-by-continent-600x537.png 600w\" sizes=\"(max-width: 633px) 100vw, 633px\" \/><\/a><\/p>\n<p>As we all know, the Asian continents account for around 59% of the total global population, followed by Africa and Europe.<\/p>\n<h3>Visualizing Top 10 Countries by World Population Percentage<\/h3>\n<p>Another interesting analysis is how much the top 10 most populous countries contribute to the global population.<br \/>\nTo do so, you can create a bar graph that displays countries\/territories and their corresponding world population percentages.<\/p>\n<div class=\"clipboard\">\n<pre><code class=\"language-python\">top_countries = population_data.sort_values('World_Population_Percentage', ascending=False).head(10)\n\nplt.figure(figsize=(10, 8))\nplt.barh(top_countries['Country_Territory'],\n         top_countries['World_Population_Percentage'],\n         color='skyblue')\n\nplt.xlabel('% of world population')\nplt.title('Top 10 Countries by World Population %')\nplt.gca().invert_yaxis()  # Ensure the largest value is at the top\nplt.show()<\/code><\/pre>\n<\/div>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/griddb.net\/wp-content\/uploads\/2024\/05\/img5-barplot-of-top-10-countries-by-population-perc.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/griddb.net\/wp-content\/uploads\/2024\/05\/img5-barplot-of-top-10-countries-by-population-perc.png\" alt=\"\" width=\"941\" height=\"697\" class=\"aligncenter size-full wp-image-30100\" srcset=\"\/wp-content\/uploads\/2024\/05\/img5-barplot-of-top-10-countries-by-population-perc.png 941w, \/wp-content\/uploads\/2024\/05\/img5-barplot-of-top-10-countries-by-population-perc-300x222.png 300w, \/wp-content\/uploads\/2024\/05\/img5-barplot-of-top-10-countries-by-population-perc-768x569.png 768w, \/wp-content\/uploads\/2024\/05\/img5-barplot-of-top-10-countries-by-population-perc-600x444.png 600w\" sizes=\"(max-width: 941px) 100vw, 941px\" \/><\/a><\/p>\n<p>The above output shows that China and India each contribute around 17% of the global population. So, every third person on the globe is either an Indian or a Chinese.<\/p>\n<h3>Analyzing the Top 10 Most Dense Countries\/Territories<\/h3>\n<p>In addition to total population, another exciting metric to analyze is population density, which refers to the population within a specific area (per kilometer square in our dataset).<\/p>\n<p>This can tell us how congested a certain country or territory is. To retrieve this information, we can plot a bar graph that displays countries with the highest population density, as shown in the following script.<\/p>\n<div class=\"clipboard\">\n<pre><code class=\"language-python\">top_density_countries = population_data.nlargest(10, 'Density__per_km__')\n\nplt.figure(figsize=(8, 6))\nsns.barplot(x='Country_Territory', y='Density__per_km__', data=top_density_countries)\nplt.xticks(rotation=45)\nplt.title('Top 10 Countries\/Territories by Population Density per Sq-km')\nplt.xlabel('Country')\nplt.ylabel('Density (per km\u00c2\u00b2)')\nplt.show()<\/code><\/pre>\n<\/div>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/griddb.net\/wp-content\/uploads\/2024\/05\/img6-bar-plot-of-top-10-countries-by-population-density.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/griddb.net\/wp-content\/uploads\/2024\/05\/img6-bar-plot-of-top-10-countries-by-population-density.png\" alt=\"\" width=\"911\" height=\"770\" class=\"aligncenter size-full wp-image-30101\" srcset=\"\/wp-content\/uploads\/2024\/05\/img6-bar-plot-of-top-10-countries-by-population-density.png 911w, \/wp-content\/uploads\/2024\/05\/img6-bar-plot-of-top-10-countries-by-population-density-300x254.png 300w, \/wp-content\/uploads\/2024\/05\/img6-bar-plot-of-top-10-countries-by-population-density-768x649.png 768w, \/wp-content\/uploads\/2024\/05\/img6-bar-plot-of-top-10-countries-by-population-density-600x507.png 600w\" sizes=\"(max-width: 911px) 100vw, 911px\" \/><\/a><\/p>\n<p>The output shows that Macau (a Chinese territory) has the highest population density (around 27 thousand) per square kilometer, followed by Monaco and Singapore.<\/p>\n<h3>Top 10 Most Densely Populated Countries in Europe and Asia<\/h3>\n<p>Let&#8217;s dig deeper and plot two bar plots for Asia and Europe, displaying the top ten countries\/territories with the highest population density within these continents.<\/p>\n<div class=\"clipboard\">\n<pre><code class=\"language-python\">europe = population_data[population_data['Continent'] == 'Europe'].sort_values('Density__per_km__', ascending=False).head(10)\nsouth_america = population_data[population_data['Continent'] == 'South America'].sort_values('Density__per_km__', ascending=False).head(10)\n\nfig, axes = plt.subplots(1, 2, figsize=(14, 6))\n\naxes[0].barh(europe['Country_Territory'], europe['Density__per_km__'], color='skyblue')\naxes[0].set_title('Top 10 Densely Populated Countries\/Territories in Europe')\naxes[0].invert_yaxis()\n\naxes[1].barh(south_america['Country_Territory'], south_america['Density__per_km__'], color='lightgreen')\naxes[1].set_title('Top 10 Densely Populated Countries\/Territories in South America')\naxes[1].invert_yaxis()  \n\nplt.tight_layout()\nplt.show()<\/code><\/pre>\n<\/div>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/griddb.net\/wp-content\/uploads\/2024\/05\/img7-bar-plots-of-asian-and-european-countries-by-population-density.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/griddb.net\/wp-content\/uploads\/2024\/05\/img7-bar-plots-of-asian-and-european-countries-by-population-density.png\" alt=\"\" width=\"1377\" height=\"591\" class=\"aligncenter size-full wp-image-30102\" srcset=\"\/wp-content\/uploads\/2024\/05\/img7-bar-plots-of-asian-and-european-countries-by-population-density.png 1377w, \/wp-content\/uploads\/2024\/05\/img7-bar-plots-of-asian-and-european-countries-by-population-density-300x129.png 300w, \/wp-content\/uploads\/2024\/05\/img7-bar-plots-of-asian-and-european-countries-by-population-density-1024x439.png 1024w, \/wp-content\/uploads\/2024\/05\/img7-bar-plots-of-asian-and-european-countries-by-population-density-768x330.png 768w, \/wp-content\/uploads\/2024\/05\/img7-bar-plots-of-asian-and-european-countries-by-population-density-600x258.png 600w\" sizes=\"(max-width: 1377px) 100vw, 1377px\" \/><\/a><\/p>\n<p>As you can see, Monaco is the most densely populated territory in Europe, while Ecuador is the most densely populated country in South America.<\/p>\n<h3>Population Growth from 1970 to 2022 for Top 10 Most Populous Countries<\/h3>\n<p>Another exciting aspect of the world population is the population growth rate. This can help identify countries that need to control their population.<\/p>\n<p>For example, using a line plot, the following script shows the population growth rates for the top ten most populous countries from 1970 to 2022.<\/p>\n<div class=\"clipboard\">\n<pre><code class=\"language-python\">top_countries = population_data.sort_values(by='World_Population_Percentage', ascending=False).head(10)\n\ndata = pd.melt(top_countries,\n               id_vars=['Country_Territory'],\n               value_vars=['1970_Population', '1980_Population', '1990_Population', '2000_Population', '2010_Population', '2015_Population', '2020_Population', '2022_Population'],\n               var_name='Year',\n               value_name='Population')\n\ndata['Year'] = data['Year'].str.extract('(d+)').astype(int)\n\n\nplt.figure(figsize=(9, 6))\nsns.line plot(x='Year',\n             y='Population',\n             hue='Country_Territory',\n             data=data, marker='o')\n\nplt.title('Population Growth from 1970 to 2022 for Top 10 Most Populous Countries')\nplt.legend(title='Country')\nplt.show()<\/code><\/pre>\n<\/div>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/griddb.net\/wp-content\/uploads\/2024\/05\/img8-line-plot-of-population-growth-of-top-10-countries.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/griddb.net\/wp-content\/uploads\/2024\/05\/img8-line-plot-of-population-growth-of-top-10-countries.png\" alt=\"\" width=\"958\" height=\"687\" class=\"aligncenter size-full wp-image-30103\" srcset=\"\/wp-content\/uploads\/2024\/05\/img8-line-plot-of-population-growth-of-top-10-countries.png 958w, \/wp-content\/uploads\/2024\/05\/img8-line-plot-of-population-growth-of-top-10-countries-300x215.png 300w, \/wp-content\/uploads\/2024\/05\/img8-line-plot-of-population-growth-of-top-10-countries-768x551.png 768w, \/wp-content\/uploads\/2024\/05\/img8-line-plot-of-population-growth-of-top-10-countries-370x265.png 370w, \/wp-content\/uploads\/2024\/05\/img8-line-plot-of-population-growth-of-top-10-countries-600x430.png 600w\" sizes=\"(max-width: 958px) 100vw, 958px\" \/><\/a><\/p>\n<p>The above output shows that the population difference between India and China was around 200 million in 1970, which has narrowed to almost negligible by the end of 2022.<\/p>\n<p>These are some of the interesting observations from the world population dataset. You can further analyze and visualize the dataset using the same techniques to extract more exciting information.<\/p>\n<h2>Updating GridDB Data Using SQL UPDATE QUERY<\/h2>\n<p>In addition to inserting and retrieving data, GridDB allows you to update existing data easily. To do so, select the row and column you want to update and assign the updated value to the selected cell.<\/p>\n<p>For example, you can use the following script to update the value in the <code>Capital<\/code> column where the <code>Country_Territory<\/code> column contains <code>Afghanistan<\/code>. The script first selects the row where the <code>Country_Territory<\/code> column equals <code>Afghanistan<\/code> and then selects the index of the <code>Capital<\/code> column using our Pandas dataframe. The script then updates the cell value using the <code>update()<\/code> method. Finally, the container&#8217;s <code>commit()<\/code> function commits the change to the container in the GridDB.<\/p>\n<div class=\"clipboard\">\n<pre><code class=\"language-python\">population_container.set_auto_commit(False)\nquery = population_container.query(\"SELECT * WHERE Country_Territory = 'Afghanistan'\")\nrs = query.fetch(True)  \n\nif rs.has_next():\n    data = rs.next()\n    capital_index = population_data.columns.get_loc('Capital')\n\n    data[capital_index] = 'Kabool'\n    rs.update(data)\n\npopulation_container.commit()\nprint(\"Capital name updated successfully.\")\n\nquery = population_container.query(\"select *\")\nrs = query.fetch()\npopulation_data = rs.fetch_rows()\npopulation_data.head()<\/code><\/pre>\n<\/div>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/griddb.net\/wp-content\/uploads\/2024\/05\/img9-dataset-after-updating-a-record.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/griddb.net\/wp-content\/uploads\/2024\/05\/img9-dataset-after-updating-a-record.png\" alt=\"\" width=\"1132\" height=\"340\" class=\"aligncenter size-full wp-image-30104\" srcset=\"\/wp-content\/uploads\/2024\/05\/img9-dataset-after-updating-a-record.png 1132w, \/wp-content\/uploads\/2024\/05\/img9-dataset-after-updating-a-record-300x90.png 300w, \/wp-content\/uploads\/2024\/05\/img9-dataset-after-updating-a-record-1024x308.png 1024w, \/wp-content\/uploads\/2024\/05\/img9-dataset-after-updating-a-record-768x231.png 768w, \/wp-content\/uploads\/2024\/05\/img9-dataset-after-updating-a-record-600x180.png 600w\" sizes=\"(max-width: 1132px) 100vw, 1132px\" \/><\/a><\/p>\n<h2>Deleting GridDB Data Using SQL DELETE QUERY<\/h2>\n<p>Similarly, you can use the <code>remove()<\/code> method of the object returned by the <code>fetch()<\/code> query to remove a row from a GridDB container. The following script demonstrates removing all rows where the <code>Continent<\/code> column contains the value <code>Europe<\/code>.<\/p>\n<div class=\"clipboard\">\n<pre><code class=\"language-python\">population_container.set_auto_commit(False)\n\nquery = population_container.query(\"SELECT * WHERE Continent = 'Europe'\")\n\nrs = query.fetch(True)\n\nwhile rs.has_next():\n    data = rs.next()\n    rs.remove()\n\n\npopulation_container.commit()\nprint(\"Records where Continent is 'Europe' have been successfully deleted.\")\n\nquery = population_container.query(\"select *\")\nrs = query.fetch()\npopulation_data = rs.fetch_rows()\npopulation_data.head()<\/code><\/pre>\n<\/div>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/griddb.net\/wp-content\/uploads\/2024\/05\/img10-dataset-after-deleting-a-record.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/griddb.net\/wp-content\/uploads\/2024\/05\/img10-dataset-after-deleting-a-record.png\" alt=\"\" width=\"1122\" height=\"333\" class=\"aligncenter size-full wp-image-30105\" srcset=\"\/wp-content\/uploads\/2024\/05\/img10-dataset-after-deleting-a-record.png 1122w, \/wp-content\/uploads\/2024\/05\/img10-dataset-after-deleting-a-record-300x89.png 300w, \/wp-content\/uploads\/2024\/05\/img10-dataset-after-deleting-a-record-1024x304.png 1024w, \/wp-content\/uploads\/2024\/05\/img10-dataset-after-deleting-a-record-768x228.png 768w, \/wp-content\/uploads\/2024\/05\/img10-dataset-after-deleting-a-record-600x178.png 600w\" sizes=\"(max-width: 1122px) 100vw, 1122px\" \/><\/a><\/p>\n<h2>Conclusion<\/h2>\n<p>In this article, you analyze the world population dataset using Python with GridDB as the database. The analysis revealed interesting information about the world population.In addition, you learned how to connect Python to the GridDB database. You saw how to insert CSV data into a GridDB container and then fetch data from a GridDB container into a Pandas dataframe.<\/p>\n<p>GridDB is a highly scaleable open-source database that allows you to efficiently store and retrieve all varieties of data using NoSQL and traditional SQL queries. GridDB database is highly recommended for managing IoT and time-series datasets.<\/p>\n<p>You can find the <a href=\"https:\/\/bit.ly\/4a3IaE0\">Jupyter Notebook for this blog on Github<\/a>.<\/p>\n<p>If you have any questions or queries related to GridDB, <a href=\"https:\/\/stackoverflow.com\/questions\/ask?tags=griddb\">create a Stackoverflow post<\/a>. Remember to use the <code>griddb<\/code> tag so our engineers can respond quickly.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, you will learn how to visualize world population data in Python using GridDB as the database. You will use the GridDB Python client to connect with a GridDB database, insert world population CSV data into GridDB, retrieve this data, and plot different visualizations using Python matplotlib, pandas, and seaborn libraries. Note: The [&hellip;]<\/p>\n","protected":false},"author":41,"featured_media":30103,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[121],"tags":[],"class_list":["post-46800","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.1.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Analyzing World Population Data in Python | GridDB: Open Source Time Series Database for IoT<\/title>\n<meta name=\"description\" content=\"In this article, you will learn how to visualize world population data in Python using GridDB as the database. You will use the GridDB Python client to\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/griddb.net\/en\/blog\/analyzing-world-population-data-in-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Analyzing World Population Data in Python | GridDB: Open Source Time Series Database for IoT\" \/>\n<meta property=\"og:description\" content=\"In this article, you will learn how to visualize world population data in Python using GridDB as the database. You will use the GridDB Python client to\" \/>\n<meta property=\"og:url\" content=\"https:\/\/griddb.net\/en\/blog\/analyzing-world-population-data-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"GridDB: Open Source Time Series Database for IoT\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/griddbcommunity\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-05-16T07:00:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-11-13T20:56:59+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.griddb.net\/wp-content\/uploads\/2024\/05\/img8-line-plot-of-population-growth-of-top-10-countries.png\" \/>\n\t<meta property=\"og:image:width\" content=\"958\" \/>\n\t<meta property=\"og:image:height\" content=\"687\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"griddb-admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@GridDBCommunity\" \/>\n<meta name=\"twitter:site\" content=\"@GridDBCommunity\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"griddb-admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"12 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/griddb.net\/en\/blog\/analyzing-world-population-data-in-python\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/griddb.net\/en\/blog\/analyzing-world-population-data-in-python\/\"},\"author\":{\"name\":\"griddb-admin\",\"@id\":\"https:\/\/www.griddb.net\/en\/#\/schema\/person\/4fe914ca9576878e82f5e8dd3ba52233\"},\"headline\":\"Analyzing World Population Data in Python\",\"datePublished\":\"2024-05-16T07:00:00+00:00\",\"dateModified\":\"2025-11-13T20:56:59+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/griddb.net\/en\/blog\/analyzing-world-population-data-in-python\/\"},\"wordCount\":1564,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.griddb.net\/en\/#organization\"},\"image\":{\"@id\":\"https:\/\/griddb.net\/en\/blog\/analyzing-world-population-data-in-python\/#primaryimage\"},\"thumbnailUrl\":\"\/wp-content\/uploads\/2024\/05\/img8-line-plot-of-population-growth-of-top-10-countries.png\",\"articleSection\":[\"Blog\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/griddb.net\/en\/blog\/analyzing-world-population-data-in-python\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/griddb.net\/en\/blog\/analyzing-world-population-data-in-python\/\",\"url\":\"https:\/\/griddb.net\/en\/blog\/analyzing-world-population-data-in-python\/\",\"name\":\"Analyzing World Population Data in Python | GridDB: Open Source Time Series Database for IoT\",\"isPartOf\":{\"@id\":\"https:\/\/www.griddb.net\/en\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/griddb.net\/en\/blog\/analyzing-world-population-data-in-python\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/griddb.net\/en\/blog\/analyzing-world-population-data-in-python\/#primaryimage\"},\"thumbnailUrl\":\"\/wp-content\/uploads\/2024\/05\/img8-line-plot-of-population-growth-of-top-10-countries.png\",\"datePublished\":\"2024-05-16T07:00:00+00:00\",\"dateModified\":\"2025-11-13T20:56:59+00:00\",\"description\":\"In this article, you will learn how to visualize world population data in Python using GridDB as the database. You will use the GridDB Python client to\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/griddb.net\/en\/blog\/analyzing-world-population-data-in-python\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/griddb.net\/en\/blog\/analyzing-world-population-data-in-python\/#primaryimage\",\"url\":\"\/wp-content\/uploads\/2024\/05\/img8-line-plot-of-population-growth-of-top-10-countries.png\",\"contentUrl\":\"\/wp-content\/uploads\/2024\/05\/img8-line-plot-of-population-growth-of-top-10-countries.png\",\"width\":958,\"height\":687},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.griddb.net\/en\/#website\",\"url\":\"https:\/\/www.griddb.net\/en\/\",\"name\":\"GridDB: Open Source Time Series Database for IoT\",\"description\":\"GridDB is an open source time-series database with the performance of NoSQL and convenience of SQL\",\"publisher\":{\"@id\":\"https:\/\/www.griddb.net\/en\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.griddb.net\/en\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.griddb.net\/en\/#organization\",\"name\":\"Fixstars\",\"url\":\"https:\/\/www.griddb.net\/en\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.griddb.net\/en\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/griddb.net\/wp-content\/uploads\/2019\/04\/fixstars_logo_web_tagline.png\",\"contentUrl\":\"https:\/\/griddb.net\/wp-content\/uploads\/2019\/04\/fixstars_logo_web_tagline.png\",\"width\":200,\"height\":83,\"caption\":\"Fixstars\"},\"image\":{\"@id\":\"https:\/\/www.griddb.net\/en\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/griddbcommunity\/\",\"https:\/\/x.com\/GridDBCommunity\",\"https:\/\/www.linkedin.com\/company\/griddb-by-toshiba\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.griddb.net\/en\/#\/schema\/person\/4fe914ca9576878e82f5e8dd3ba52233\",\"name\":\"griddb-admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.griddb.net\/en\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/5bceca1cafc06886a7ba873e2f0a28011a1176c4dea59709f735b63ae30d0342?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/5bceca1cafc06886a7ba873e2f0a28011a1176c4dea59709f735b63ae30d0342?s=96&d=mm&r=g\",\"caption\":\"griddb-admin\"},\"url\":\"https:\/\/www.griddb.net\/en\/author\/griddb-admin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Analyzing World Population Data in Python | GridDB: Open Source Time Series Database for IoT","description":"In this article, you will learn how to visualize world population data in Python using GridDB as the database. You will use the GridDB Python client to","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/griddb.net\/en\/blog\/analyzing-world-population-data-in-python\/","og_locale":"en_US","og_type":"article","og_title":"Analyzing World Population Data in Python | GridDB: Open Source Time Series Database for IoT","og_description":"In this article, you will learn how to visualize world population data in Python using GridDB as the database. You will use the GridDB Python client to","og_url":"https:\/\/griddb.net\/en\/blog\/analyzing-world-population-data-in-python\/","og_site_name":"GridDB: Open Source Time Series Database for IoT","article_publisher":"https:\/\/www.facebook.com\/griddbcommunity\/","article_published_time":"2024-05-16T07:00:00+00:00","article_modified_time":"2025-11-13T20:56:59+00:00","og_image":[{"width":958,"height":687,"url":"https:\/\/www.griddb.net\/wp-content\/uploads\/2024\/05\/img8-line-plot-of-population-growth-of-top-10-countries.png","type":"image\/png"}],"author":"griddb-admin","twitter_card":"summary_large_image","twitter_creator":"@GridDBCommunity","twitter_site":"@GridDBCommunity","twitter_misc":{"Written by":"griddb-admin","Est. reading time":"12 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/griddb.net\/en\/blog\/analyzing-world-population-data-in-python\/#article","isPartOf":{"@id":"https:\/\/griddb.net\/en\/blog\/analyzing-world-population-data-in-python\/"},"author":{"name":"griddb-admin","@id":"https:\/\/www.griddb.net\/en\/#\/schema\/person\/4fe914ca9576878e82f5e8dd3ba52233"},"headline":"Analyzing World Population Data in Python","datePublished":"2024-05-16T07:00:00+00:00","dateModified":"2025-11-13T20:56:59+00:00","mainEntityOfPage":{"@id":"https:\/\/griddb.net\/en\/blog\/analyzing-world-population-data-in-python\/"},"wordCount":1564,"commentCount":0,"publisher":{"@id":"https:\/\/www.griddb.net\/en\/#organization"},"image":{"@id":"https:\/\/griddb.net\/en\/blog\/analyzing-world-population-data-in-python\/#primaryimage"},"thumbnailUrl":"\/wp-content\/uploads\/2024\/05\/img8-line-plot-of-population-growth-of-top-10-countries.png","articleSection":["Blog"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/griddb.net\/en\/blog\/analyzing-world-population-data-in-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/griddb.net\/en\/blog\/analyzing-world-population-data-in-python\/","url":"https:\/\/griddb.net\/en\/blog\/analyzing-world-population-data-in-python\/","name":"Analyzing World Population Data in Python | GridDB: Open Source Time Series Database for IoT","isPartOf":{"@id":"https:\/\/www.griddb.net\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/griddb.net\/en\/blog\/analyzing-world-population-data-in-python\/#primaryimage"},"image":{"@id":"https:\/\/griddb.net\/en\/blog\/analyzing-world-population-data-in-python\/#primaryimage"},"thumbnailUrl":"\/wp-content\/uploads\/2024\/05\/img8-line-plot-of-population-growth-of-top-10-countries.png","datePublished":"2024-05-16T07:00:00+00:00","dateModified":"2025-11-13T20:56:59+00:00","description":"In this article, you will learn how to visualize world population data in Python using GridDB as the database. You will use the GridDB Python client to","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/griddb.net\/en\/blog\/analyzing-world-population-data-in-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/griddb.net\/en\/blog\/analyzing-world-population-data-in-python\/#primaryimage","url":"\/wp-content\/uploads\/2024\/05\/img8-line-plot-of-population-growth-of-top-10-countries.png","contentUrl":"\/wp-content\/uploads\/2024\/05\/img8-line-plot-of-population-growth-of-top-10-countries.png","width":958,"height":687},{"@type":"WebSite","@id":"https:\/\/www.griddb.net\/en\/#website","url":"https:\/\/www.griddb.net\/en\/","name":"GridDB: Open Source Time Series Database for IoT","description":"GridDB is an open source time-series database with the performance of NoSQL and convenience of SQL","publisher":{"@id":"https:\/\/www.griddb.net\/en\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.griddb.net\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.griddb.net\/en\/#organization","name":"Fixstars","url":"https:\/\/www.griddb.net\/en\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.griddb.net\/en\/#\/schema\/logo\/image\/","url":"https:\/\/griddb.net\/wp-content\/uploads\/2019\/04\/fixstars_logo_web_tagline.png","contentUrl":"https:\/\/griddb.net\/wp-content\/uploads\/2019\/04\/fixstars_logo_web_tagline.png","width":200,"height":83,"caption":"Fixstars"},"image":{"@id":"https:\/\/www.griddb.net\/en\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/griddbcommunity\/","https:\/\/x.com\/GridDBCommunity","https:\/\/www.linkedin.com\/company\/griddb-by-toshiba"]},{"@type":"Person","@id":"https:\/\/www.griddb.net\/en\/#\/schema\/person\/4fe914ca9576878e82f5e8dd3ba52233","name":"griddb-admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.griddb.net\/en\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/5bceca1cafc06886a7ba873e2f0a28011a1176c4dea59709f735b63ae30d0342?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5bceca1cafc06886a7ba873e2f0a28011a1176c4dea59709f735b63ae30d0342?s=96&d=mm&r=g","caption":"griddb-admin"},"url":"https:\/\/www.griddb.net\/en\/author\/griddb-admin\/"}]}},"_links":{"self":[{"href":"https:\/\/www.griddb.net\/en\/wp-json\/wp\/v2\/posts\/46800","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.griddb.net\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.griddb.net\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.griddb.net\/en\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/www.griddb.net\/en\/wp-json\/wp\/v2\/comments?post=46800"}],"version-history":[{"count":1,"href":"https:\/\/www.griddb.net\/en\/wp-json\/wp\/v2\/posts\/46800\/revisions"}],"predecessor-version":[{"id":51462,"href":"https:\/\/www.griddb.net\/en\/wp-json\/wp\/v2\/posts\/46800\/revisions\/51462"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.griddb.net\/en\/wp-json\/wp\/v2\/media\/30103"}],"wp:attachment":[{"href":"https:\/\/www.griddb.net\/en\/wp-json\/wp\/v2\/media?parent=46800"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.griddb.net\/en\/wp-json\/wp\/v2\/categories?post=46800"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.griddb.net\/en\/wp-json\/wp\/v2\/tags?post=46800"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}