Download Link: https://blog.finxter.com/wp-content/uploads/2023/04/Finxter_OpenAI_Python_API.pdf
Tag: Programming
Python Snake Made Easy
This code creates a simple Snake game using the Pygame library in Python.
The game starts with a small snake moving around the screen, controlled by arrow keys, eating randomly placed food items. Each time the snake eats food, it grows longer, and the game gets faster.
The game ends if the snake collides with itself or the screen borders, and the player’s goal is to keep the snake growing as long as possible.
import pygame import sys import random # Initialize pygame pygame.init() # Set screen dimensions WIDTH = 640 HEIGHT = 480 CELL_SIZE = 20 # Colors WHITE = (255, 255, 255) GREEN = (0, 255, 0) RED = (255, 0, 0) # Create the game screen screen = pygame.display.set_mode((WIDTH, HEIGHT)) # Set the game clock clock = pygame.time.Clock() def random_food_position(): return (random.randint(0, (WIDTH-CELL_SIZE)//CELL_SIZE) * CELL_SIZE, random.randint(0, (HEIGHT-CELL_SIZE)//CELL_SIZE) * CELL_SIZE) def draw_snake(snake_positions): for pos in snake_positions: pygame.draw.rect(screen, GREEN, pygame.Rect(pos[0], pos[1], CELL_SIZE, CELL_SIZE)) def draw_food(food_position): pygame.draw.rect(screen, RED, pygame.Rect(food_position[0], food_position[1], CELL_SIZE, CELL_SIZE)) def main(): snake_positions = [(100, 100), (80, 100), (60, 100)] snake_direction = (20, 0) food_position = random_food_position() game_speed = 10 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_UP and snake_direction != (0, 20): snake_direction = (0, -20) elif event.key == pygame.K_DOWN and snake_direction != (0, -20): snake_direction = (0, 20) elif event.key == pygame.K_LEFT and snake_direction != (20, 0): snake_direction = (-20, 0) elif event.key == pygame.K_RIGHT and snake_direction != (-20, 0): snake_direction = (20, 0) new_head = (snake_positions[0][0] + snake_direction[0], snake_positions[0][1] + snake_direction[1]) if new_head in snake_positions or new_head[0] < 0 or new_head[0] >= WIDTH or new_head[1] < 0 or new_head[1] >= HEIGHT: break snake_positions.insert(0, new_head) if new_head == food_position: food_position = random_food_position() game_speed += 1 else: snake_positions.pop() screen.fill(WHITE) draw_snake(snake_positions) draw_food(food_position) pygame.display.update() clock.tick(game_speed) if __name__ == "__main__": main()
This simple implementation of the classic Snake game uses the Pygame library in Python — here’s how I quickly lost in the game:

Here’s a brief explanation of each part of the code:
- Import libraries:
pygamefor creating the game window and handling events,sysfor system-specific parameters and functions, andrandomfor generating random numbers.
- Initialize
pygamewithpygame.init(). - Set screen dimensions and cell size. The
WIDTH,HEIGHT, andCELL_SIZEconstants define the game window size and the size of each cell (both snake segments and food). - Define color constants
WHITE,GREEN, andREDas RGB tuples. - Create the game screen with
pygame.display.set_mode((WIDTH, HEIGHT)). - Set the game clock with
pygame.time.Clock()to control the game’s frame rate. - Define the
random_food_position()function to generate a random food position on the grid, ensuring it’s aligned with theCELL_SIZE. - Define
draw_snake(snake_positions)function to draw the snake’s body segments at their respective positions using green rectangles. - Define
draw_food(food_position)function to draw the food as a red rectangle at its position. - Define the
main()function, which contains the main game loop and logic.- Initialize the snake’s starting position, direction, food position, and game speed.
- The
while Trueloop is the main game loop that runs indefinitely until the snake collides with itself or the screen borders. - Handle events like closing the game window or changing the snake’s direction using arrow keys.
- Update the snake’s position based on its current direction.
- Check for collisions with itself or the screen borders, breaking the loop if a collision occurs.
- If the snake’s head is at the food position, generate a new food position and increase the game speed. Otherwise, remove the last snake segment.
- Update the game display by filling the screen with a white background, drawing the snake and food, and updating the display.
- Control the game speed using
clock.tick(game_speed).
- Run the
main()function when the script is executed by checking if__name__ == "__main__".
Python Web Scraping: From URL to CSV in No Time
Setting up the Environment
Before diving into web scraping with Python, set up your environment by installing the necessary libraries.
First, install the following libraries: requests, BeautifulSoup, and pandas. These packages play a crucial role in web scraping, each serving different purposes.
To install these libraries, click on the previously provided links for a full guide (including troubleshooting) or simply run the following commands:
pip install requests
pip install beautifulsoup4
pip install pandas
The requests library will be used to make HTTP requests to websites and download the HTML content. It simplifies the process of fetching web content in Python.
BeautifulSoup is a fantastic library that helps extract data from the HTML content fetched from websites. It makes navigating, searching, and modifying HTML easy, making web scraping straightforward and convenient.
Pandas will be helpful in data manipulation and organizing the scraped data into a CSV file. It provides powerful tools for working with structured data, making it popular among data scientists and web scraping enthusiasts. 
Fetching and Parsing URL

Next, you’ll learn how to fetch and parse URLs using Python to scrape data and save it as a CSV file. We will cover sending HTTP requests, handling errors, and utilizing libraries to make the process efficient and smooth. 
Sending HTTP Requests
When fetching content from a URL, Python offers a powerful library known as the requests library. It allows users to send HTTP requests, such as GET or POST, to a specific URL, obtain a response, and parse it for information.
We will use the requests library to help us fetch data from our desired URL.
For example:
import requests
response = requests.get('https://example.com/data.csv')
The variable response will store the server’s response, including the data we want to scrape. From here, we can access the content using response.content, which will return the raw data in bytes format. 
Handling HTTP Errors
Handling HTTP errors while fetching data from URLs ensures a smooth experience and prevents unexpected issues. The requests library makes error handling easy by providing methods to check whether the request was successful.
Here’s a simple example:
import requests
response = requests.get('https://example.com/data.csv')
response.raise_for_status()
The raise_for_status() method will raise an exception if there’s an HTTP error, such as a 404 Not Found or 500 Internal Server Error. This helps us ensure that our script doesn’t continue to process erroneous data, allowing us to gracefully handle any issues that may arise. 
With these tools, you are now better equipped to fetch and parse URLs using Python. This will enable you to effectively scrape data and save it as a CSV file. 
Extracting Data from HTML

In this section, we’ll discuss extracting data from HTML using Python. The focus will be on utilizing the BeautifulSoup library, locating elements by their tags, and attributes. 
Using BeautifulSoup
BeautifulSoup is a popular Python library that simplifies web scraping tasks by making it easy to parse and navigate through HTML. To get started, import the library and request the page content you want to scrape, then create a BeautifulSoup object to parse the data:
from bs4 import BeautifulSoup import requests url = "example_website" response = requests.get(url) soup = BeautifulSoup(response.text, "html.parser")
Now you have a BeautifulSoup object and can start extracting data from the HTML. 
Locating Elements by Tags and Attributes
BeautifulSoup provides various methods to locate elements by their tags and attributes. Some common methods include find(), find_all(), select(), and select_one().
Let’s see these methods in action:
# Find the first <span> tag
span_tag = soup.find("span") # Find all <span> tags
all_span_tags = soup.find_all("span") # Locate elements using CSS selectors
title = soup.select_one("title") # Find all <a> tags with the "href" attribute
links = soup.find_all("a", {"href": True})
These methods allow you to easily navigate and extract data from an HTML structure. 
Once you have located the HTML elements containing the needed data, you can extract the text and attributes.
Here’s how:
# Extract text from a tag text = span_tag.text # Extract an attribute value url = links[0]["href"]
Finally, to save the extracted data into a CSV file, you can use Python’s built-in csv module. 
import csv # Writing extracted data to a CSV file
with open("output.csv", "w", newline="") as csvfile: writer = csv.writer(csvfile) writer.writerow(["Index", "Title"]) for index, link in enumerate(links, start=1): writer.writerow([index, link.text])
Following these steps, you can successfully extract data from HTML using Python and BeautifulSoup, and save it as a CSV file. 
Recommended: Basketball Statistics β Page Scraping Using Python and BeautifulSoup
Organizing Data

This section explains how to create a dictionary to store the scraped data and how to write the organized data into a CSV file. 
Creating a Dictionary
Begin by defining an empty dictionary that will store the extracted data elements.
In this case, the focus is on quotes, authors, and any associated tags. Each extracted element should have its key, and the value should be a list that contains individual instances of that element.
For example:
data = { "quotes": [], "authors": [], "tags": []
}
As you scrape the data, append each item to its respective list. This approach makes the information easy to index and retrieve when needed. 
Working with DataFrames and Pandas
Once the data is stored in a dictionary, it’s time to convert it into a dataframe. Using the Pandas library, it’s easy to transform the dictionary into a dataframe where the keys become the column names and the respective lists become the rows.
Simply use the following command:
import pandas as pd df = pd.DataFrame(data)
Exporting Data to a CSV File
With the dataframe prepared, it’s time to write it to a CSV file. Thankfully, Pandas comes to the rescue once again. Using the dataframe’s built-in .to_csv() method, it’s possible to create a CSV file from the dataframe, like this:
df.to_csv('scraped_data.csv', index=False)
This command will generate a CSV file called 'scraped_data.csv' containing the organized data with columns for quotes, authors, and tags. The index=False parameter ensures that the dataframe’s index isn’t added as an additional column. 
Recommended: 17 Ways to Read a CSV File to a Pandas DataFrame
And there you have itβa neat, organized CSV file containing your scraped data!
Handling Pagination

This section will discuss how to handle pagination while scraping data from multiple URLs using Python to save the extracted content in a CSV format. It is essential to manage pagination effectively because most websites display their content across several pages.
Looping Through Web Pages
Looping through web pages requires the developer to identify a pattern in the URLs, which can assist in iterating over them seamlessly. Typically, this pattern would include the page number as a variable, making it easy to adjust during the scraping process.
Once the pattern is identified, you can use a for loop to iterate over a range of page numbers. For each iteration, update the URL with the page number and then proceed with the scraping process. This method allows you to extract data from multiple pages systematically.
For instance, let’s consider that the base URL for every page is "https://www.example.com/listing?page=", where the page number is appended to the end.
Here is a Python example that demonstrates handling pagination when working with such URLs:
import requests
from bs4 import BeautifulSoup
import csv base_url = "https://www.example.com/listing?page=" with open("scraped_data.csv", "w", newline="") as csvfile: csv_writer = csv.writer(csvfile) csv_writer.writerow(["Data_Title", "Data_Content"]) # Header row for page_number in range(1, 6): # Loop through page numbers 1 to 5 url = base_url + str(page_number) response = requests.get(url) soup = BeautifulSoup(response.text, "html.parser") # TODO: Add scraping logic here and write the content to CSV file.
In this example, the script iterates through the first five pages of the website and writes the scraped content to a CSV file. Note that you will need to implement the actual scraping logic (e.g., extracting the desired content using Beautiful Soup) based on the website’s structure.
Handling pagination with Python allows you to collect more comprehensive data sets
, improving the overall success of your web scraping efforts. Make sure to respect the website’s robots.txt rules and rate limits to ensure responsible data collection.
Exporting Data to CSV

You can export web scraping data to a CSV file in Python using the Python CSV module and the Pandas to_csv function.
Both approaches are widely used and efficiently handle large amounts of data.
Python CSV Module
The Python CSV module is a built-in library that offers functionalities to read from and write to CSV files. It is simple and easy to use
. To begin with, first, import the csv module.
import csv
To write the scraped data to a CSV file, open the file in write mode ('w') with a specified file name, create a CSV writer object, and write the data using the writerow() or writerows() methods as required.
with open('data.csv', 'w', newline='') as file: writer = csv.writer(file) writer.writerow(["header1", "header2", "header3"]) writer.writerows(scraped_data)
In this example, the header row is written first, followed by the rows of data obtained through web scraping. 
Using Pandas to_csv()
Another alternative is the powerful library Pandas, often used in data manipulation and analysis. To use it, start by importing the Pandas library.
import pandas as pd
Pandas offers the to_csv() method, which can be applied to a DataFrame. If you have web-scraped data and stored it in a DataFrame, you can easily export it to a CSV file with the to_csv() method, as shown below:
dataframe.to_csv('data.csv', index=False)
In this example, the index parameter is set to False to exclude the DataFrame index from the CSV file.
The Pandas library also provides options for handling missing values, date formatting, and customizing separators and delimiters, making it a versatile choice for data export.
10 Minutes to Pandas in 5 Minutes

If you’re just getting started with Pandas, I’d recommend you check out our free blog guide (it’s only 5 minutes!): 
Recommended: 5 Minutes to Pandas — A Simple Helpful Guide to the Most Important Pandas Concepts (+ Cheat Sheet)
Python π Put Legend Outside Plot π β Easy Guide
Are you tired of feeling boxed in by your Python plots and ready to break free from the constraints of traditional legend placement?
In this guide, I’ll show you how to put legends outside your plot for (click to
jump):
Let’s start with the first! 

Matplotlib Put Legend Outside Plot
Let’s start with various ways to position the legend outside for better visualization and presentation.
Matplotlib Set Legend Outside Plot (General)
First, let’s adjust the legend’s position outside the plot in general. To do this, use the bbox_to_anchor parameter in the legend function like this: matplotlib.pyplot.legend(bbox_to_anchor=(x, y))
. Here, adjust the values of x and y to control the legend’s position.

import matplotlib.pyplot as plt # Sample data for plotting
x = [1, 2, 3, 4, 5]
y1 = [1, 4, 9, 16, 25]
y2 = [1, 8, 27, 64, 125] # Create the plot
plt.plot(x, y1, label='y = x^2')
plt.plot(x, y2, label='y = x^3') # Set the legend's position outside the plot using bbox_to_anchor
plt.legend(bbox_to_anchor=(1.05, 1)) # Add axis labels and title
plt.xlabel('x')
plt.ylabel('y')
plt.title('Plotting with External Legend') # Display the plot
plt.show()
In this example, the bbox_to_anchor parameter is set to (1.05, 1), which moves the legend slightly to the right of the plot.
Info: The bbox_to_anchor parameter in matplotlib.pyplot.legend() uses a tuple of two values, x and y, to control the position of the legend. x=0/1 controls the left/right and y=0/1 controls the bottom/top legend placement.
Generally, in axes coordinates:
(0, 0)represents the left-bottom corner of the axes.(0, 1)represents the left-top corner of the axes.(1, 0)represents the right-bottom corner of the axes.(1, 1)represents the right-top corner of the axes.
However, the values of x and y are not limited to the range [0, 1]. You can use values outside of this range to place the legend beyond the axes’ boundaries.
For example:
- (1.05, 1) places the legend slightly to the right of the top-right corner of the axes.
- (0, 1.1) places the legend slightly above the top-left corner of the axes.
Using negative values is also allowed. For example:
- (-0.3, 0) places the legend to the left of the bottom-left corner of the axes.
- (1, -0.2) places the legend below the bottom-right corner of the axes.
The range of x and y depends on the desired position of the legend relative to the plot. By adjusting these values, you can fine-tune the legend’s position to create the perfect visualization.
Matplotlib Set Legend Below or Above Plot
To place the legend below the plot, you can set the loc parameter as ‘upper center’ and use bbox_to_anchor like this: plt.legend(loc='upper center', bbox_to_anchor=(0.5, -0.1)). For placing the legend above the plot, use bbox_to_anchor=(0.5, 1.1) instead
.

Matplotlib Set Legend Left of Plot (Upper, Center, Lower Left)
For positioning the legend to the left of the plot, use the following examples:
- Upper left:
plt.legend(loc='center left', bbox_to_anchor=(-0.2, 0.5))
- Center left:
plt.legend(loc='center left', bbox_to_anchor=(-0.1, 0.5)) - Lower left:
plt.legend(loc='lower left', bbox_to_anchor=(-0.2, 0))
Matplotlib Set Legend Right of Plot (Upper, Center, Lower Right)
To position the legend to the right of the plot, you can try the following:
- Upper right:
plt.legend(loc='upper right', bbox_to_anchor=(1.1, 1))
- Center right:
plt.legend(loc='center right', bbox_to_anchor=(1.1, 0.5)) - Lower right:
plt.legend(loc='lower right', bbox_to_anchor=(1.1, 0))

Matplotlib Set Subplots Legend Outside Plot
When working with subplots, you can place a single, unified legend outside the plot by iterating over the axes and using the legend() method on the last axis (source)
. Remember to use the bbox_to_anchor parameter to control the legend’s position.
Here’s an example that does two things, i.e., (1) placing the legend on the right and (2) adjusting the layout to accommodate the external legend:

import numpy as np
import matplotlib.pyplot as plt # Sample data for plotting
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x) # Create subplots
fig, axes = plt.subplots(nrows=2, ncols=1, sharex=True) # Plot data on the first subplot
axes[0].plot(x, y1, label='sin(x)')
axes[0].set_title('Sine Wave') # Plot data on the second subplot
axes[1].plot(x, y2, label='cos(x)', color='orange')
axes[1].set_title('Cosine Wave') # Set a common x-label for both subplots
fig.text(0.5, 0.04, 'x', ha='center') # Set y-labels for individual subplots
axes[0].set_ylabel('sin(x)')
axes[1].set_ylabel('cos(x)') # Create a unified legend for both subplots
handles, labels = axes[-1].get_legend_handles_labels()
for ax in axes[:-1]: h, l = ax.get_legend_handles_labels() handles += h labels += l # Place the unified legend outside the plot using bbox_to_anchor
fig.legend(handles, labels, loc='upper right', bbox_to_anchor=(1, 0.75)) # Adjust the layout to accommodate the external legend
fig.subplots_adjust(right=0.7) # Display the subplots
plt.show()
Legend Outside Plot Is Cut Off
If your legend is cut off, you can adjust the saved figure’s dimensions using plt.savefig('filename.ext', bbox_inches='tight'). The bbox_inches parameter with the value ‘tight’ will ensure that the whole legend is visible on the saved figure
.
Add Legend Outside a Scatter Plot
For a scatter plot, you can use the same approach as mentioned earlier by adding the loc and bbox_to_anchor parameters to position the legend outside the plot. For instance, plt.legend(loc='upper right', bbox_to_anchor=(1.1, 1)) will place the legend in the upper right corner outside the scatter plot
.
If your legend is cut off when placing it outside the plot in Python’s Matplotlib, you can adjust the layout and save or display the entire figure, including the external legend, by following these steps:
- Use the
bbox_to_anchorparameter in thelegend()function to control the position of the legend. - Adjust the layout of the figure using the
subplots_adjust()ortight_layout()function to make room for the legend. - Save or display the entire figure, including the external legend.
Here’s an example demonstrating these steps:

import matplotlib.pyplot as plt # Sample data for plotting
x = [1, 2, 3, 4, 5]
y1 = [1, 4, 9, 16, 25]
y2 = [1, 8, 27, 64, 125] # Create the plot
plt.plot(x, y1, label='y = x^2')
plt.plot(x, y2, label='y = x^3') # Set the legend's position outside the plot using bbox_to_anchor
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left') # Add axis labels and title
plt.xlabel('x')
plt.ylabel('y')
plt.title('Plotting with External Legend') # Adjust the layout to accommodate the external legend
plt.subplots_adjust(right=0.7) # Display the plot
plt.show()
In this example, you use the subplots_adjust() function to adjust the layout of the figure and make room for the legend.
You can also use the tight_layout() function, which automatically adjusts the layout based on the elements in the figure:

# ...
# Set the legend's position outside the plot using bbox_to_anchor
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left') # Add axis labels and title
plt.xlabel('x')
plt.ylabel('y')
plt.title('Plotting with External Legend') # Automatically adjust the layout to accommodate the external legend
plt.tight_layout(rect=[0, 0, 0.7, 1]) # Display the plot
plt.show()
In this case, the rect parameter is a list [left, bottom, right, top], which specifies the normalized figure coordinates of the new bounding box for the subplots. Adjust the values in the rect parameter as needed to ensure the legend is not cut off.
Additional Legend Configurations
In this section, you’ll learn about some additional ways to customize the legend for your plots in Python using Matplotlib. This will help you create more meaningful and visually appealing visualizations. However, feel free to skip ahead to the following sections on plotting the legend outside the figure in Seaborn, Pandas, and Bokeh.
Python Set Legend Position
As you already know by now, you can place the legend at a specific position in the plot by using the bbox_to_anchor parameter. For example, you could place the legend outside the plot to the right by passing (1.05, 0.5) as the argument:
import matplotlib.pyplot as plt plt.legend(bbox_to_anchor=(1.05, 0.5))
This will place the legend slightly outside the right border of the plot, with its vertical center aligned with the plot center.
Python Set Legend Location
You can easily change the location of the legend by using the loc parameter. Matplotlib allows you to use predefined locations like 'upper right', 'lower left', etc., or use a specific coordinate by passing a tuple:
plt.legend(loc='upper left')
This will place the legend in the upper-left corner of the plot.
Python Set Legend Font Size
To change the font size of the legend, you can use the fontsize parameter. You can pass a numeric value or a string like 'small', 'medium', or 'large' to set the font size:
plt.legend(fontsize='large')
This will increase the font size of the legend text.
Python Set Legend Title
If you want to add a title to your legend, you can use the title parameter. Just pass a string as the argument to set the title:
plt.legend(title='My Legend Title')
This will add the title “My Legend Title” above your legend.
Python Set Legend Labels
If you’d like to customize the legend labels, you can pass the labels parameter. It takes a list of strings as the argument:
plt.legend(labels=['Label 1', 'Label 2'])
Your legend will now display the custom labels “Label 1” and “Label 2” for the corresponding plot elements.
Python Set Legend Color
Changing the color of the legend text and lines can be achieved by using the labelcolor parameter. Just pass a color string or a list of colors:
plt.legend(labelcolor='red')
This will change the color of the legend text and lines to red.
Seaborn Put Legend Outside Plot
In this section, I’ll show you how to move the legend outside the plot using Seaborn. Let’s dive into various ways of setting the legend position, like below, above, left or right of the plot.
Sns Set Legend Outside Plot (General)
First, let’s talk about the general approach:
You can use the legend() function and the bbox_to_anchor parameter from Matplotlib to move the legend. You can combine this with the loc parameter to fine-tune the legend’s position.
Here’s a quick example:

import seaborn as sns
import matplotlib.pyplot as plt # Sample data for plotting
tips = sns.load_dataset("tips") # Create a Seaborn plot with axis variable 'ax'
fig, ax = plt.subplots()
sns_plot = sns.scatterplot(x="total_bill", y="tip", hue="day", data=tips, ax=ax) # Set the legend's position outside the plot using bbox_to_anchor on 'ax'
ax.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0.) # Add axis labels and title
ax.set_xlabel('Total Bill')
ax.set_ylabel('Tip')
ax.set_title('Tips by Total Bill and Day') # Display the plot
plt.show()
Sns Set Legend Below or Above Plot
Now let’s move the legend below or above the plot.
Simply adjust the bbox_to_anchor parameter accordingly. For example, to place the legend below the plot, you can use bbox_to_anchor=(0.5, -0.1):
ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.1))
And to place the legend above the plot, use bbox_to_anchor=(0.5, 1.1):
ax.legend(loc='lower center', bbox_to_anchor=(0.5, 1.1))
Sns Set Legend Left of Plot (Upper, Center, Lower Left)
Similarly, to position the legend on the left side of the plot, you can use the following code snippets:
Upper left:
ax.legend(loc='upper right', bbox_to_anchor=(-0.15, 1))
Center left:
ax.legend(loc='center right', bbox_to_anchor=(-0.15, 0.5))
Lower left:
ax.legend(loc='lower right', bbox_to_anchor=(-0.15, 0))
Sns Set Legend Right of Plot (Upper, Center, Lower Right)
Lastly, to place the legend on the right side of the plot, adjust the bbox_to_anchor parameter like so:
Upper right:
ax.legend(loc='upper left', bbox_to_anchor=(1.05, 1))
Center right:
ax.legend(loc='center left', bbox_to_anchor=(1.05, 0.5))
Lower right:
ax.legend(loc='lower left', bbox_to_anchor=(1.05, 0))
With these techniques, you can easily position the legend outside the plot using Seaborn! Happy plotting!
Pandas Put Legend Outside Plot
When working with Pandas and Matplotlib, you often want to move the legend outside the plot
to improve readability. No worries! You can do this by taking advantage of the fact that .plot() returns a Matplotlib axis, enabling you to add .legend(bbox_to_anchor=(x, y)) to your code.
Here’s how:

First, import the necessary libraries like Pandas and Matplotlib:
import pandas as pd import matplotlib.pyplot as plt
Create your DataFrame and plot it using the plot() function, like this:
data = {'A': [1, 2, 3], 'B': [4, 5, 6]}
df = pd.DataFrame(data)
ax = df.plot()
Next, you’ll want to place the legend outside the plot. Adjust the coordinates parameter, bbox_to_anchor, to position it according to your preferences. For example, if you want to place the legend to the right of the plot, use:
ax.legend(bbox_to_anchor=(1.05, 1)) plt.tight_layout() plt.show()
This code will place the legend to the right of the plot, at the top
.
Bokeh Put Legend Outside Plot
Placing a legend outside the plot in Bokeh can be easily done by using the add_layout method. You’ll need to create a Legend object manually and then add it to your plot using add_layout. This gives you the flexibility to position the legend anywhere on your plot, which is particularly helpful when you have many curves that might be obscured by an overlapping legend. 
Here’s a short example to help you move the legend outside the plot in Bokeh:

from bokeh.plotting import figure, show from bokeh.models import Legend, LegendItem from bokeh.io import output_notebook output_notebook() # Example data x = list(range(10)) y1 = [i**2 for i in x] y2 = [i**0.5 for i in x] # Create a figure p = figure(title="Example Plot") # Add line glyphs and store their renderer r1 = p.line(x, y1, line_color="blue", legend_label="Line 1") r2 = p.line(x, y2, line_color="red", legend_label="Line 2") # Create Legend object legend = Legend(items=[ LegendItem(label="Line 1", renderers=[r1]), LegendItem(label="Line 2", renderers=[r2]) ], location="top_left") # Add legend to plot p.add_layout(legend, 'right') # Show plot show(p)
To ensure your plot is as clear as possible, we recommend experimenting with different legend positions and layouts. By customizing your plot, you can maintain a clean and organized display of your curves even when there’s a lot of information to convey.
With Bokeh, you have full control over your plot’s appearance, so enjoy exploring different options to find the perfect fit for your data! 

Keep Learning With Your Python Cheat Sheet! 
Feel free to download our free cheat sheet and join our tech and coding academy by downloading the free Finxter cheat sheets — it’s fun!
Python List of Tuples to DataFrame πΌ
To convert a list of tuples to a Pandas DataFrame, import the pandas library, call the DataFrame constructor, and pass the list of tuples as the data argument such as in pd.DataFrame(tuples_list, columns=['Number', 'Letter']).
Here’s a minimal example:
import pandas as pd tuples_list = [(1, 'A'), (2, 'B'), (3, 'C')] df = pd.DataFrame(tuples_list, columns=['Number', 'Letter'])
The output of the given code will be a Pandas DataFrame with two columns, 'Number' and 'Letter', as follows:
Number Letter
0 1 A
1 2 B
2 3 C
After the Panda image, let’s dive deeper into this conversion technique so you can improve your skills and learn more on Pandas’ assume capabilities!
I’ll also show you how to convert a list of named tuples — and how to convert the DataFrame back to a list of tuples (key-value pairs). 

Converting a List of Tuples to DataFrame
First, let’s explore how to convert a list of tuples into a DataFrame using Python’s Pandas library. 
Using DataFrame Constructor
The simplest way to convert a list of tuples into a DataFrame is by using the DataFrame() constructor provided by the Pandas library. This method is straightforward and can be achieved in just a few lines of code.
Here’s an example:
import pandas as pd
tuple_list = [('A', 1), ('B', 2), ('C', 3)]
df = pd.DataFrame(tuple_list)
print(df)
Executing this code will create a DataFrame with the following structure:
| 0 | 1 |
| A | 1 |
| B | 2 |
| C | 3 |
Handling Data with Column Names
When converting a list of tuples to a DataFrame, it’s often useful to include column names to make the data more readable and understandable. To do this, you can add the columns parameter when calling the DataFrame() constructor.
Here’s an example:
import pandas as pd
tuple_list = [('A', 1), ('B', 2), ('C', 3)]
column_names = ['Letter', 'Number']
df = pd.DataFrame(tuple_list, columns=column_names)
print(df)
With the column names specified, the resulting DataFrame will look like this:
| Letter | Number |
| A | 1 |
| B | 2 |
| C | 3 |
By using the DataFrame constructor and handling data with column names, you can easily convert a list of tuples into a DataFrame that is more organized and easier to understand. Keep working with these techniques, and soon enough, you’ll be a master of DataFrames! 
Examples and Use Cases

When working with Python, one often encounters data stored in lists of tuples. These data structures are lightweight and easy to use, but sometimes, it’s beneficial to convert them into a more structured format, such as a DataFrame
. In this section, we will explore some examples and use cases for converting a list of tuples into a DataFrame in Python, using the pandas library.
Here’s a simple example that demonstrates how to create a DataFrame from a list of tuples:
import pandas as pd data = [('Peter', 18, 7), ('Riff', 15, 6), ('John', 17, 8), ('Michel', 18, 7), ('Sheli', 17, 5)]
df = pd.DataFrame(data, columns=['Name', 'Age', 'Score'])
In this example, we have a list of tuples representing student data, with each tuple containing a name, age, and score. By passing this list to the DataFrame constructor along with the column names, we can easily convert it into a DataFrame
.
Consider another use case, where we need to filter and manipulate data before converting it into a DataFrame. For instance, let’s imagine we have a list of sales data, with each tuple representing an item, its price, and the number of sales:
data = [('Item A', 35, 20), ('Item B', 45, 15), ('Item C', 50, 30), ('Item D', 25, 10)]
In this case, we can use list comprehensions to filter items with sales greater than 20 and update the price by applying a 10% discount:
filtered_data = [(item, price * 0.9, sales) for item, price, sales in data if sales > 20] df = pd.DataFrame(filtered_data, columns=['Item', 'Discounted Price', 'Sales'])
Now, our DataFrame contains only the filtered items with the discounted prices
.
Python List of Named Tuples to DataFrame

Converting a list of named tuples to a DataFrame in Python can be done efficiently using the pandas library’s default functions as well.
Info: A named tuple is a subclass of a tuple, which allows you to access elements by name, making it highly readable and practical for data manipulation.
First, create a list of named tuples using Python’s built-in collections module.
Let’s assume we have a list of students with their names, ages, and test scores:
from collections import namedtuple Student = namedtuple('Student', ['name', 'age', 'score'])
students = [ Student('Alice', 23, 89), Student('Bob', 22, 92), Student('Charlie', 24, 85)
]
With the list of named tuples prepared, proceed to import the pandas library and use the pd.DataFrame() method to convert the list to a DataFrame:
import pandas as pd dataframe = pd.DataFrame(students, columns=Student._fields)
This process creates a DataFrame with columns corresponding to the named tuple fields. The final result appears as follows:
name age score
0 Alice 23 89
1 Bob 22 92
2 Charlie 24 85
In summary, simply define the list with the named tuple structure, and then call the pd.DataFrame() method to create the DataFrame.
Create a List of Tuples From a DataFrame

When working with data in Python, you may need to convert a DataFrame back into a list of tuples.
To begin, import the library in your Python code using import pandas as pd.
Now, let’s say you have a DataFrame, and you want to extract its data as a list of tuples. The simplest approach is to use the itertuples() function, which is a built-in method in Pandas (source).
To use this method, call the itertuples() function on the DataFrame object, and then pass the output to the list() function to convert it into a list:
python import pandas as pd # Sample DataFrame data = {'Name': ['John', 'Alice', 'Tim'], 'Age': [28, 22, 27]}
df = pd.DataFrame(data) # Convert DataFrame to list of tuples list_of_tuples = list(df.itertuples(index=False, name=None))
print(list_of_tuples)
This code will output:
[('John', 28), ('Alice', 22), ('Tim', 27)]
The itertuples() method has two optional parameters: index and name. Setting index=False excludes the DataFrame index from the tuples, and setting name=None returns regular tuples instead of named tuples.
So there you go! You now know how to convert a DataFrame into a list of tuples using the Pandas library in Python
. To keep learning and improving your Python skills, feel free to download our cheat sheets and visit the recommended Pandas tutorial:
Recommended: 10 Minutes to Pandas (in 5 Minutes)
How to Transfer an ENS Domain: A Quick and Easy Guide
Prerequisites
Before beginning the process of transferring an ENS domain, ensure you have the following tools at hand. This includes
:
- A current Ethereum wallet: This is the wallet containing the ENS domain that you want to transfer.
- A new Ethereum wallet: This is the wallet to which you want to transfer the ENS domain.
- Access to the ENS domain manager: This is where you’ll manage your ENS domain settings and perform the transfer (app.ens.domains).
Note that ENS domains work as non-fungible tokens (NFTs), so transferring ownership is similar to transferring other NFTs
.
Attention: Simply sending the ENS domain NFT to the other wallet will only transfer the Registrant but not the Controller role of the domain. The controller manages ENS records and subdomains, while the registrant controls the controller address and registration transfers. Roughly speaking, the registrant is more powerful than the controller. But the controller is the address that gets the assets! For example, if the controller of domain aaa.eth has address 0x123 and the registrant has address 0x456 and you send funds to aaa.eth, the controller will get those funds but the registrant has ultimate control if they choose to.
Make sure you have control over both the current and new Ethereum wallets.
With these prerequisites in place, you are well-prepared to proceed with the transfer
. Just follow the steps in the subsequent sections and your ENS domain will soon be transferred to your new Ethereum wallet.
Connecting Wallet

Before transferring your ENS domain, connect your wallet to the platform you’ll be using for the process. This is typically done on the ENS Domain Manager or other compatible platforms like Coinbase Wallet and MetaMask.
First, visit the chosen platform’s website or open its app and log in using your wallet credentials. Most platforms support popular wallets like MetaMask or Coinbase Wallet. 
Once you’ve logged in, navigate to the settings or account section of the platform. Here, you should find an option to connect your wallet. Select the option and follow the on-screen instructions to complete your wallet connection. Some platforms may require additional verification steps, such as providing password authentication or approving the connection from your connected wallet. 
After successfully connecting your wallet, you should have access to your ENS domain and be ready to transfer it to a new wallet. Connecting your wallet is a crucial step in transferring your ENS domain, as it ensures the proper ownership and control of your domain during the process. 
Finding ENS Domain
In order to transfer an ENS domain, the first step is finding the desired domain. Fortunately, there are user-friendly tools that make this process simple and efficient. 

The ENS Domain Manager application can be used for finding and managing domains. Simply visit the application and search for the desired domain to check its availability.
Once the domain is found, users can view additional details, such as the current owner, registration process, and more. The ENS domain system also offers compatibility with IPFS by including hashes in the domain records. This feature enables decentralized websites to be hosted seamlessly. 
In order to complete domain-related actions smoothly, it is essential to have an Ethereum wallet connected, such as MetaMask. This connection allows for proper authentication and execution of transactions in the Ethereum Name Service ecosystem. 
What is the Difference Between a Registrant and a Controller?

The distinction between a Registrant and a Controller in an Ethereum Name Service (ENS) allows for a more efficient management of domain names
. To understand their roles, let’s start with a brief explanation of each.
A Registrant is the person or entity to whom the domain is registered
. They are the ultimate owner of the domain and have complete control over it. The Registrant can transfer ownership to another account or a smart contract that manages subdomains, records, and more, while still being able to recover ownership if needed (ENS Documentation).
On the other hand, a Controller is someone who has been delegated with day-to-day control over the domain by the Registrant
. This role can change the resolver and add or edit records. Some applications, like Fleek and OpenSea, set themselves as the Controller to update records on behalf of the Registrant (ENS App). A Controller’s role is similar to the operator of DNS servers for a domain name that is registered with a domain registrar like GoDaddy (Reddit ethdev).
The Registrant has the ultimate control over the name, while the Controller is responsible for handling everyday operations. Separating these roles makes it easier to build automated systems to update ENS efficiently and provide more flexibility in domain management
.
Initiating Transfer Process

You can transfer the ENS domain to a new wallet in three steps:
Step 1: Connect your wallet that has both registrant and controller roles to the ENS website.
Step 2: Transfer the Registrant role by clicking the first Transfer button and confirming the transaction proposed by your wallet. This will cost some ETH fees because it is recorded in the blockchain. Make sure you have some ETH in your wallet for fees!
Step 3: Transfer the Controller role by clicking the second Transfer button and confirming the transaction.
Now you’re done! The new wallet address now has the ENS NFT and both the controller and registrant roles. 
Verifying Transfer

After completing the process of transferring your ENS domain, it’s important to verify that the transfer has been successfully executed
. This section will guide you through the steps to make sure everything went smoothly.
First and foremost, check your new wallet and make sure it now displays the transferred ENS domain (as an NFT). If the domain is visible, it’s a clear indication that the transfer has been successful
. However, if the domain is not visible, do not panic; it might take a few minutes for the changes to reflect on the blockchain. Just give it some time
.
In case you still cannot see the domain in the new wallet after a reasonable waiting period, head back to the ENS App and enter your ENS domain name in the search bar. This will provide you with detailed information, including the current Registrant and Controller addresses
. Verify that these two addresses match the new wallet address that you intended to transfer the domain to. If they match, then the transfer has been successful, and you just need to wait a bit longer for your new wallet to reflect the changes.
You can also verify on an Ethereum blockchain explorer such as https://etherscan.io/ (see previous graphic).
Remember to keep track of any errors or irregularities you encounter during the process. In the rare case that you experience an issue that you cannot resolve, consider reaching out to the ENS support team or community forums for assistance
. They’re always ready to help you with any problems related to ENS domain transfers.
Common Issues and Troubleshooting

When transferring an ENS domain, users may encounter some common issues. In this section, we’ll discuss a few of these problems and provide solutions to help make the process smoother. 
One common issue is forgetting to change the Controller of the domain. The controller is the account that manages day-to-day operations of the domain, such as creating subdomains and setting resolvers
. To resolve this, visit the ENS Domain Manager in your wallet and update the controller address.
Another issue that may arise is the inability to transfer the domain due to it being locked
. This can occur if the domain is involved in a dispute or if it has been involved in illegal activities. To resolve this, contact the ENS administrators or an appropriate legal process for assistance.
Users might also face challenges in maintaining anonymity while transferring a domain. To maintain privacy, it is recommended to use an anonymous wallet or take additional steps like holding a fake sale on OpenSea and keeping the new address segregated from KYC services
. More details can be found here.
Lastly, technical difficulties may occur while using the ENS Domain Manager. In such cases, ensure that you are using an updated browser version
, have a stable internet connection
, and consider trying a different browser or device if issues persist.
If you want to keep learning about exponential technologies, consider joining our free email academy (140,000 coders and tech enthusiasts). However, you should only join if you want to learn coding and tech!
Recommended: Ethereum β Top 10 Articles to Get Started
How to Get a File from Your EC2 Instance to Your Local Computer
I am running AutoGPT on an EC2 instance and encountered the following problem:
Challenge: How to pull a file (e.g., an Image) from the EC2 instance to my local machine (Windows, Mac, Linux)?
In this quick article, I share my findings! If you’re short on time, you can use these commands to exchange files between your local machine and your EC2 instance:
Example 1: Transfer File from EC2 to your computer
scp -i /path/to/your/ec2key.pem user@instance-ip:/path/to/your/file /path/to/local/destinationExample 2: Transfer File from your computer to EC2
scp -i /path/to/your/ec2key.pem /path/to/local/file user@instance-ip:/path/to/remote/file
I’ll explain them in more detail below! 
Prerequisites
Before attempting to transfer a file from an EC2 instance to your local computer, there are a few essential prerequisites you need to have in place
:
- EC2 key: The
ec2key.pemfile was created when you set up the EC2 instance. Make sure you have access to it. - EC2 username and IP: Find this information in the EC2 Console using the ‘Connect to Instance’ button. These are essential for establishing a secure connection to your instance. If you already have the .pem file, you don’t need this.
- Public DNS name: Obtain the public DNS for your instance from the Amazon EC2 console or by using the AWS CLI. Find it in the Public IPv4 DNS column of the Instances pane.
- File path: Note the exact path to the file you want to transfer from the EC2 instance to your local machine. This information is necessary for initiating the file transfer process
.
With these prerequisites in place, you’ll be prepared to seamlessly transfer files between your EC2 instance and local computer! 
Connecting to EC2 Instance

Being able to connect to your Amazon EC2 instance is crucial for effectively accessing and transferring files between your local computer and the instance. In this section, you’ll learn where to find instance information and how to set up the SSH client for secure connection
.
Finding Instance Information
First, you need to gather essential information about your EC2 instance, including the instance ID, public DNS, and the key pair file you created when launching the EC2 instance. You can find this information in the Amazon EC2 console.
Simply navigate to the Instances section, then select the instance you want to connect to and click on the ‘Connect’ button.
You will find user-friendly instructions on how to access your instance with your preferred method, as well as the necessary details
. Remember to keep your key pair file safe and secure, as it’s required for authentication.
Setting Up SSH Client
With the instance information in hand, you can now set up an SSH client to establish a secure connection to your EC2 instance.
OpenSSH and PuTTY are two popular SSH clients for Windows, while Mac and Linux users can use their built-in terminal applications for SSH connections
.
If you’re using OpenSSH or the default terminal on Mac/Linux, you’ll need to use the following command, adjusting the path to your key pair file and the instance details as needed:
$ ssh -i /path/to/your-ec2-key.pem username@IPExample:![]()
ssh -i '.\AWS Key Pair.pem' ec2-user@12.34.567.89
Windows users with PuTTY can follow these instructions to load their key pair file, enter the public DNS, and start an SSH session to the EC2 instance
.
Now that you’re connected to your EC2 instance, you can navigate its file system and transfer files without a hitch
. In the next section, you’ll learn how to get a file from your EC2 instance to your local computer, step by step. Stay tuned!
Transferring Files

Transferring files from an EC2 instance to a local computer can be done with ease using either SCP (Secure Copy) commands or SFTP (SSH File Transfer Protocol) clients. Let’s explore both methods to see how they work. 
Using SCP (Secure Copy) Commands
SCP provides a simple method for transferring files between your local computer and an EC2 instance. To use SCP, ensure that you have the required information such as your EC2 key pair (.pem file), your EC2 instance’s IP address, and the file path of the file you wish to transfer. 

Here’s an example of how to use the SCP command:
Example 1: Transfer File from EC2 to your computer
scp -i /path/to/your/ec2key.pem user@instance-ip:/path/to/your/file /path/to/local/destinationExample 2: Transfer File from your computer to EC2
scp -i /path/to/your/ec2key.pem /path/to/local/file user@instance-ip:/path/to/remote/file
This will download the file from your EC2 instance to your local computer securely. Just replace the paths and user information with your own. 
Let’s dive a bit deeper into these commands so you understand the different components:
Example 1: Transfer File from EC2 to your computer
scp -i /path/to/your/ec2key.pem user@instance-ip:/path/to/your/file /path/to/local/destination
This command transfers a file from an Amazon EC2 (Elastic Compute Cloud) instance to your local computer. Here’s a breakdown of the command components:
scp: The command itself, which stands for “secure copy”.-i /path/to/your/ec2key.pem: The-iflag is followed by the path to your EC2 private key file (usually in.pemformat), which is used for authentication when connecting to the EC2 instance.user@instance-ip: This specifies the username and the IP address (or DNS name) of the EC2 instance you want to connect to./path/to/your/file: The path to the file you want to transfer from the EC2 instance./path/to/local/destination: The path to the location on your local computer where you want to save the transferred file.
Example 2: Transfer File from your computer to EC2
scp -i /path/to/your/ec2key.pem /path/to/local/file user@instance-ip:/path/to/remote/file
This command transfers a file from your local computer to an Amazon EC2 instance. The structure of this command is similar to the first example:
scp: The command itself, which stands for “secure copy”.-i /path/to/your/ec2key.pem: The-iflag is followed by the path to your EC2 private key file (usually in.pemformat), which is used for authentication when connecting to the EC2 instance./path/to/local/file: The path to the file on your local computer that you want to transfer.user@instance-ip: This specifies the username and the IP address (or DNS name) of the EC2 instance you want to connect to./path/to/remote/file: The path to the location on the EC2 instance where you want to save the transferred file.
So far so good. Next, you’ll learn about an alternative to transfer files in a remote EC2 setting, i.e., SFTP. However, I recommend the previous approach using SCP.
Using SFTP (SSH File Transfer Protocol) Clients
SFTP allows for file transfer between a local computer and an EC2 instance through an intuitive graphical user interface. Popular SFTP clients include FileZilla, WinSCP, and Cyberduck. These clients make it simple to drag and drop files from your local machine to your remote server. 

To connect to your EC2 instance, you’ll need the following information:
- Server: your EC2 instance’s IP address

- Username: your EC2 username (usually “ec2-user”)

- Password: leave this field empty, and use your key pair file instead

Simply input the required information into your SFTP client, and you’ll be able to transfer files between your local computer and EC2 instance in a matter of seconds! 
Common Issues and Troubleshooting Tips

When attempting to transfer files from an EC2 instance to a local computer, it’s not uncommon to come across some hurdles along the way. In this section, we will discuss some common issues users might face and provide troubleshooting tips to help you overcome them. 
One common issue that users might encounter is having difficulty connecting to the EC2 instance. To address this issue, ensure that your instance is running and has passed its status checks. Make sure you’re using the correct key, username, and IP address you obtained from the EC2 console. If the problem persists, check the instance’s security group rules, and ensure that the necessary ports are open for communication. 
Another problem that may arise is slow or interrupted file transfers. To solve this, ensure that your internet connection is stable and consider using a file transfer tool like scp or FileZilla that supports resuming interrupted transfers. Additionally, compressing the files before transferring can help speed up the process. 
If you’re facing issues with file permissions while transferring files from an EC2 instance, make sure you have the necessary read and write permissions on both the local and remote systems. You might need to adjust the permissions on your EC2 instance or your local machine to successfully transfer the files. 
Lastly, if you’re troubleshooting EC2 Windows instance issues, you can use the EC2Rescue tool to help diagnose and fix common issues. This tool can be run using different methods, including the GUI, the command line interface (CLI), or the AWSSupport-RunEC2RescueForWindowsTool Systems Manager Run Command. 
Keep SSH Session Alive
Problem Formulation
I just tried to run AutoGPT on an EC2 instance using SSH from my local Windows machine. But here’s the annoying part: the connection always closes and AutoGPT can only work in small ~10 minute increments. When I return to my machine, I need to SSH into my instance and restart the program.
Problem Formulation: Your SSH connection to a remote server works properly at your workplace, but it freezes after 10-15 minutes when connecting from home. You don’t receive any error messages, but you notice zombie login users that need to be killed manually.
Quick and Easy Solution (Client-Side)
To prevent an SSH connection from closing when the client goes silent, you can configure the client to send a keep-alive signal to the server periodically.
Create a configuration file in your home directory at $HOME/.ssh/config, and set its permissions to 600 using after file creation. To send a keep-alive signal every 240 seconds, for example, add the following lines to the configuration file:chmod 600 ~/.ssh/config
Host * ServerAliveInterval 240
You can get this done with the following two commands on Linux:
printf '%s\n' 'Host *' ' ServerAliveInterval 240' > ~/.ssh/config
chmod 600 ~/.ssh/config
You can then check the file content using the command cat ~/.ssh/config like so:

Alternative Solution: Server Side
In some cases, you have access to the server’s SSH settings. In that case, add an entry ClientAliveInterval 60 to the file /etc/ssh/sshd_config. I used the Vim editor in the terminal to accomplish this.

Do you want to keep improving your coding and tech skills? Feel free to check out our Python and tech academy by downloading your free cheat sheets for starters:
How I Created a Translation and Counter App using Django
In this two-part tutorial series, we will learn how to create a translation app using Django.
As an extra feature, we are going to demonstrate two ways to create a word counter that counts the number of words written.

Translating text to a language one can understand is no longer a new development. As many businesses are done on an international level, it necessitates the need to communicate in a language the other party can understand.
Advancement in technology has removed the barrier to communication. With an app such as Google Translate, you can get the meaning of text written in another language.

As part of learning Django through building projects, we are going to implement such a feature.
What We Are Going to Learn
As earlier stated, this is a two-part series project tutorial. The first part focuses on building a translation app. In the second part, we are going to learn how to add another feature, a word counter. I’m going to show you how to go about building it using both JavaScript and Python.

By the end of this tutorial, you are not only going to learn how Django interacts with web pages, but you are also going to learn how to manipulate the DOM with JavaScript. Thus, even if you have little or no knowledge of HTML, CSS, and JavaScript, you can combine your knowledge of Python with my explanation to understand what we are doing.
Getting Started
Although this is a beginner Django project, I expect you to know the steps involved in setting up Django as I don’t have to be repeating myself whenever I’m writing a Django project tutorial. However, if this is your first time, check this article to learn how to install Django.
Your Django project should be created in the current folder using the name, translator. Then use app as the name of the Django app. After installation, go to the settings.py file and add the app name.
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', # custom app 'app', ]
Creating the View Function

Next, go to the views.py file and add the following code to it.
from django.shortcuts import render
from translate import Translator # Create your views here. def index(request): if request.method == 'POST': text = request.POST['translate'] lang = request.POST['language'] translator = Translator(to_lang=lang) translation = translator.translate(text) context = { 'translation': translation, } return render(request,'index.html', context) return render(request, 'index.html')
This is a very simple view function. We get the input from the HTML form provided it is a POST request. Then, we call on the Translator class of the translator module to translate the given text into the language selected. The Translator class has an argument, to_lang. This indicates the language you want your text to be translated.
We finally use the render function to display the translated text on the index.html web page. We can as well use the HttpResponse() function but it will not be displayed on the index.html web page.
But if the request method is not POST, we simply return the web page containing an empty form.
This part is as simple as it should be. But in future tutorials, we will be dealing with a more complicated view function.
The Templates

Next is the templates folder. Create the folder and add it to the settings.py file.
TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR, 'templates')], # add these 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, },
]
Inside the folder, we create the index.html file.
<!DOCTYPE html>
<html> <head> <title>Django Translate</title> </head> <body> <h1>Django Translate App</h1> <form method="post"> {% csrf_token %} <label for="django">Enter Text:</label> <br/> <textarea onChange="" id="translate" name="translate" placeholder="Enter your text here" rows="10" cols="70"></textarea> <br> <select required name="language"> <option value="French">French</option> <option value="Spanish">Spanish</option> <option value="German">German</option> <option value="Italian">Italian</option> </select> <br> <br> <button id="btn" type="submit">Translate</button> </form> <br> <textarea id="text" placeholder="Your text will appear here" rows='10' cols='70'>{{ translation }}</textarea> </body> <script src="{% static 'js/script.js' %}"></script>
</html>
You can see the form element has the method property as POST. Without this, our view function will not work as expected. The csrf_token is a mandatory requirement for security. We use the select element to list the languages. You can add as many as you want.
Notice that the textarea and the select elements each has a name property, and if you check the view function, you will see that it has the same name as found in the HTML file. This is the way we retrieve data from the web page.
Finally, we register the URL of the app both in the project level and in the app level. For the project level, go to translate/urls.py file and add this:
from django.contrib import admin
from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('', include('app.urls'))
]
For the app level create a app/urls.py file and add this:
from django.urls import path
from .views import index urlpatterns = [ path('', index, name='home'),
]
Check the article I mentioned to see a brief explanation of the above code. That’s it. We are good to go. Fire up the local server to test what we have done.

Notice how we make the translated text appear inside the box. We accomplished this using Django templating language. Remember the context variable passed to the render() function? It is a dictionary object, the name of the key being what we passed to the textarea element. Thus, we are telling Django to display the value of the key to the web page.
And what is the value? The translated text! That is how Django dynamically writes to a web page. Feel free to play with any language of your choice provided the language is included in the translate library.
Conclusion
This is how we come to the end of the first part of this tutorial series. The full code for the project series can be found here. In the second part, we will learn two ways we can count the number of words written in the textarea element:
Recommended: How I Created a Translation and Counter App using Django (2/2)
Dictionary of Lists to DataFrame β Python Conversion
Problem Formulation
Working with Python often involves processing complex data structures such as dictionaries and lists. In many instances, it becomes necessary to convert a dictionary of lists into a more convenient and structured format like a Pandas DataFrame
.
DataFrames offer numerous benefits, including easier data handling and analysis
, as well as an array of built-in functions that make data manipulation much more straightforward.
In this context, the potential challenge arises in figuring out how to correctly convert a dictionary with lists as its values into a DataFrame. Various methods can be employed to achieve this goal, but it is crucial to understand the appropriate approach in each situation to ensure accurate and reliable data representation
.
Method 1: Using DataFrame.from_dict()

In this method, we will use the DataFrame.from_dict() function provided by the pandas library to convert a Python dictionary of lists to a DataFrame. This function is quite versatile, as it can construct a DataFrame from a dictionary of array-like or dictionaries data. 
To begin with, let’s import the necessary library:
import pandas as pd
Next, create a dictionary with lists as values. For example, let’s consider the following dictionary:
data = { 'Name': ['Sam', 'Alex', 'Jamie'], 'Age': [29, 28, 24], 'Country': ['USA', 'UK', 'Canada']
}
Now, use the from_dict() method to create a DataFrame from the dictionary. The process is quite simple, all you have to do is call the method with the dictionary as its argument. 
df = pd.DataFrame.from_dict(data)
And there you have it, a DataFrame created from a dictionary of lists! The resulting DataFrame will look like this:
Name Age Country
0 Sam 29 USA
1 Alex 28 UK
2 Jamie 24 Canada
The benefits of using this method are its simplicity and compatibility with different types of dictionary data. However, always remember to maintain a consistent length for the lists within the dictionary to avoid any issues. 
Method 2: Using pd.Series() with DataFrame

In this method, we will be using the
Pandas library’s pd.Series data structure inside the DataFrame method. It is a useful approach that can help you convert dictionaries with lists into a DataFrame format quickly and efficiently. 
To implement this method, you can use Python’s dictionary comprehension and the items() method, as shown in the syntax below:
pd.DataFrame({key: pd.Series(val) for key, val in dictionary.items()})
Here, dictionary.items() fetches key-value pairs from the dictionary, and pd.Series(val) creates a series of values from these pairs. The result is a well-structured Pandas DataFrame! 
Let’s take a look
at an example:
import pandas as pd data = { "Name": ["Alice", "Bob", "Claire"], "Age": [25, 30, 35], "City": ["London", "New York", "Sydney"],
} df = pd.DataFrame({key: pd.Series(val) for key, val in data.items()})
print(df)
Executing this code will generate the following DataFrame:
Name Age City
0 Alice 25 London
1 Bob 30 New York
2 Claire 35 Sydney
As you can see, using the pd.Series data structure with the DataFrame method provides a clean and effective way to transform your dictionaries with lists into Pandas DataFrames! 

Recommended: Python Dictionary Comprehension: A Powerful One-Liner Tutorial

Method 3: json_normalize()

In this method, we will use the pd.json_normalize function to convert a Python dict of lists to a Pandas DataFrame. This function is particularly useful for handling semi-structured nested JSON structures, as it can flatten them into flat tables.
To begin, you should first import the Pandas library using the following snippet:
import pandas as pd
Next, create your Python dict of lists like this example:
data = { 'manoj': ["java", "php", "python"], 'rajesh': ["c", "c++", "java"], 'ravi': ["r", "python", "javascript"]
}
With your data ready, you can now use the json_normalize function to convert the dict of lists into a DataFrame.
Here’s how:
df = pd.json_normalize(data, record_path='manoj', meta=['rajesh', 'ravi'])
And that’s it!
You now have a DataFrame created from the dict of lists. Don’t forget to preview your DataFrame using print(df) or df.head() to ensure that the data has been converted correctly. 
Method 4: Utilizing DataFrame Constructor with List Comprehension

In this method, we create a pandas DataFrame from a dictionary of lists using the DataFrame constructor and list comprehension. This approach is quite simple and potentially more efficient for larger datasets.
First, we need to import the pandas library:
python import pandas as pd
Next, let’s create a sample dictionary of lists containing student data:
data = { 'Name': ['Alice', 'Bob', 'Charlie'], 'Math': [80, 70, 90], 'History': [95, 85, 78] }
Now, we will use the DataFrame constructor and list comprehension to convert the dictionary of lists into a pandas DataFrame:
df = pd.DataFrame({key: pd.Series(value) for key, value in data.items()})
Here’s what’s happening in the code above:
- The dictionary of lists is iterated using
items()method to obtain the key-value pairs
- Each value is converted to a pandas Series using
pd.Series()function
- A DataFrame is created using the
pd.DataFrame()constructor to combine the converted series
Once the DataFrame is constructed, it will look something like this:
Name Math History 0 Alice 80 95 1 Bob 70 85 2 Charlie 90 78
Method 4 provides a concise and versatile way to transform a dictionary of lists into a DataFrame, making it convenient for data manipulation and analysis.
Enjoy working with it!
Summary
In this article, we explored the process of converting a Python dictionary with lists as values into a pandas DataFrame. Various methods have been discussed, such as using pd.DataFrame.from_dict() and pd.DataFrame.from_records() to achieve this. 
It’s important to choose a method that fits the specific structure and format of your data. Sometimes, you might need to preprocess the data into separate lists before creating a DataFrame. An example of doing this can be found here. 
Throughout the article, we provided examples and detailed explanations on how to work with complex data structures, including lists of lists and lists of dictionaries.
Remember to keep the code clean and efficient for better readability!
Recommended: How to Create a DataFrame From Lists?
With the knowledge gained, you’ll be better equipped to handle Python dictionaries containing lists, and successfully transform them into pandas DataFrames for a wide range of data analysis tasks. 
Happy coding!
Related Articles:
- [Collection] 11 Python Cheat Sheets Every Python Coder Must Own
- [Python OOP Cheat Sheet] A Simple Overview of Object-Oriented Programming
- [Collection] 15 Mind-Blowing Machine Learning Cheat Sheets to Pin to Your Toilet Wall
- Your 8+ Free Python Cheat Sheet [Course]
- Python Beginner Cheat Sheet: 19 Keywords Every Coder Must Know
- Python Functions and Tricks Cheat Sheet
- Python Cheat Sheet: 14 Interview Questions
- Beautiful Pandas Cheat Sheets
- 10 Best NumPy Cheat Sheets
- Python List Methods Cheat Sheet [Instant PDF Download]
- [Cheat Sheet] 6 Pillar Machine Learning Algorithms

