avatarLaxfed Paulacy

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

1623

Abstract

aw.githubusercontent.com/fivethirtyeight/data/master/nba-elo/nbaallelo.csv'</span> response = requests.<span class="hljs-built_in">get</span>(download_url)

<span class="hljs-keyword">if</span> response.status_code == <span class="hljs-number">200</span>: <span class="hljs-keyword">with</span> <span class="hljs-built_in">open</span>(<span class="hljs-string">'nba_all_elo.csv'</span>, <span class="hljs-string">'wb'</span>) <span class="hljs-keyword">as</span> f: f.<span class="hljs-built_in">write</span>(response.content)

<span class="hljs-comment"># Now the contents of the file are stored locally</span></pre></div><h2 id="9ccf">Importing the Dataset into Pandas DataFrame</h2><p id="3c07">After downloading the CSV file, you can import it into a Pandas DataFrame. To do this, import the <code>pandas</code> package and use the <code>read_csv()</code> function.</p><div id="7a9c"><pre>import pandas as pd

nba = pd.read_csv(<span class="hljs-string">'nba_all_elo.csv'</span>) <span class="hljs-comment"># Check the type of nba</span> <span class="hljs-built_in">print</span>(type(nba))

<span class="hljs-comment"># Get the length of the DataFrame</span> <span class="hljs-built_in">print</span>(len(nba))

<span class="hljs-comment"># Get the shape of the DataFrame</span> <span class="hljs-built_in">print</span>(nba.shape)

<span class="hljs-comment"># Display the first five rows of the DataFrame</span> <span class="hljs-built_in">print</span>(nba.head())

<span class="hljs-comment"># Display the last five rows of the DataFrame</span> <span class="hljs-built_in">print</span>(nba.tail())</pre></di

Options

v><h2 id="9c76">Adjusting Display Settings</h2><p id="cf44">By default, Pandas will display only a limited number of columns and rows. To adjust the display, you can set the maximum number of columns and the number of decimal places for numeric columns.</p><div id="15fe"><pre># <span class="hljs-keyword">Set</span> maximum number <span class="hljs-keyword">of</span> <span class="hljs-keyword">columns</span> <span class="hljs-keyword">to</span> be displayed pd.set_option(<span class="hljs-string">'display.max_columns'</span>, <span class="hljs-keyword">None</span>)

<span class="hljs-keyword">Set</span> the number <span class="hljs-keyword">of</span> <span class="hljs-type">decimal</span> places <span class="hljs-keyword">to</span> two

pd.set_option(<span class="hljs-string">'display.float_format'</span>, <span class="hljs-string">'{:.2f}'</span>.<span class="hljs-keyword">format</span>)</pre></div><h2 id="298f">Conclusion</h2><p id="437b">In this tutorial, you learned how to load a dataset using the <code>requests</code> package and import it into a Pandas DataFrame. Additionally, you learned about adjusting display settings for the DataFrame. These are the foundational steps that will enable you to work with datasets and perform data analysis using Python and Pandas.</p><figure id="49e0"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*v21yBqncate1-fF8.jpeg"><figcaption></figcaption></figure><p id="93c3"><a href="https://readmedium.com/python-iterating-over-containers-directly-in-python-28afa45ce3be">PYTHON — Iterating Over Containers Directly in Python</a></p></article></body>

PYTHON — Loading Dataset in Python

In the software world, the moment you start using someone else’s software, you are living in their world, under their philosophy. — Richard Stallman

Insights in this article were refined using prompt engineering methods.

PYTHON — Python Markup with reStructuredText

# Loading Dataset in Python

In this tutorial, you will learn how to load a dataset in Python using the requests package and how to import the dataset into a Pandas DataFrame. Additionally, you will explore some common operations on the DataFrame such as displaying the first and last few rows, understanding shape, and working with column formatting.

Loading Dataset using requests

To begin, you can use the requests package to download a CSV file containing basketball data from the website FiveThirtyEight. If you have Anaconda installed, requests is included in the default environment. If not, you can install it using pip.

import requests

download_url = 'https://raw.githubusercontent.com/fivethirtyeight/data/master/nba-elo/nbaallelo.csv'
response = requests.get(download_url)

if response.status_code == 200:
    with open('nba_all_elo.csv', 'wb') as f:
        f.write(response.content)

# Now the contents of the file are stored locally

Importing the Dataset into Pandas DataFrame

After downloading the CSV file, you can import it into a Pandas DataFrame. To do this, import the pandas package and use the read_csv() function.

import pandas as pd

nba = pd.read_csv('nba_all_elo.csv')
# Check the type of nba
print(type(nba))

# Get the length of the DataFrame
print(len(nba))

# Get the shape of the DataFrame
print(nba.shape)

# Display the first five rows of the DataFrame
print(nba.head())

# Display the last five rows of the DataFrame
print(nba.tail())

Adjusting Display Settings

By default, Pandas will display only a limited number of columns and rows. To adjust the display, you can set the maximum number of columns and the number of decimal places for numeric columns.

# Set maximum number of columns to be displayed
pd.set_option('display.max_columns', None)

# Set the number of decimal places to two
pd.set_option('display.float_format', '{:.2f}'.format)

Conclusion

In this tutorial, you learned how to load a dataset using the requests package and import it into a Pandas DataFrame. Additionally, you learned about adjusting display settings for the DataFrame. These are the foundational steps that will enable you to work with datasets and perform data analysis using Python and Pandas.

PYTHON — Iterating Over Containers Directly in Python

Python
ChatGPT
Loading
Dataset
Recommended from ReadMedium