Pandas — Data Input & Output

Python In-built Functions For Creating, Writing & Reading Files — #PySeries#Episode 15

J3
Jungletronics
Published in
4 min readSep 18, 2020

--

print(“Hello Pandas Data input & Output”)

DATA INPUT & OUTPUT

Working with:

. CSV
. Excel
. HTML
. Sql

Will need These

LIBRARIES:

**SQLAlchemy** — Database Abstraction Library;**lxml** — Powerful and Pythonic XML processing library combining libxml2/libxslt with the ElementTree API;**html5lib** — HTML parser based on the WHATWG HTML specification;**Beautiful Soup** — Python library designed for quick turnaround projects like screen-scraping.

For installations:

pip install SQLAlchemy
pip install lxml
pip install html5lib
pip install beautifulSoup4

Let’s get it on!

import numpy as np
import pandas as pd

CSV

Reading Writing to Files

Loading the Test File

On Colaboratory, GO TO Hamburger icon and File > Upload > ‘example.csv’ and and ‘exampl.xlsx' (download link below:), or make your own CSV file;)

pd.read_csv('example.csv')

Reading With Pandas:

df = pd.read_csv('example.csv')

Writing to a File:

df.to_csv('My_output')

pd.read_csv(‘My_output’)

Writing to a File without the index:

df.to_csv('My_output', index=False)

Reading again:

pd.read_csv('My_output')

Excel

Reading Writing to Files

pip install xlrd

Reading to a File:

pd.read_excel('example.xlsx', sheet_name='Sheet1')

Writing to a file:

df.to_excel('Example_2.xlsx', sheet_name='NewSheet1')

HTML

Reading Writing to Files

What does it mean when a bank fails?

A bank failure is the closing of a bank by a federal or state banking regulatory agency. Generally, a bank is closed when it is unable to meet its obligations to depositors and others

GOTO this link to see the HTML database to be uploaded:

Run:

data=pd.read_html(‘https://www.fdic.gov/resources/resolutions/bank-failures/failed-bank-list/banklist.html')

These are a bunch of Lists:

type(data)

Fetching the very first list element:

data[0].head()

SQL

Reading Writing to Files.

Let’s create a very simple SQL Lite Engine Database running in memory:

from sqlalchemy import create_engine
engine = create_engine('sqlite:///:memory:')
df.to_sql('my_table', engine)
sqldf=pd.read_sql('my_table', con=engine)
sqldf

There you have it! Thanks for reading! Bye!

Colab File link:)

Files link:)

Credits & References:

Jose Portilla — Python for Data Science and Machine Learning Bootcamp — Learn how to use NumPy, Pandas, Seaborn , Matplotlib , Plotly , Scikit-Learn , Machine Learning, Tensorflow , and more!

Posts Related:

00Episode#PySeries — Python — Jupiter Notebook Quick Start with VSCode — How to Set your Win10 Environment to use Jupiter Notebook

01Episode#PySeries — Python — Python 4 Engineers — Exercises! An overview of the Opportunities Offered by Python in Engineering!

02Episode#PySeries — Python — Geogebra Plus Linear Programming- We’ll Create a Geogebra program to help us with our linear programming

03Episode#PySeries — Python — Python 4 Engineers — More Exercises! — Another Round to Make Sure that Python is Really Amazing!

04Episode#PySeries — Python — Linear Regressions — The Basics — How to Understand Linear Regression Once and For All!

05Episode#PySeries — Python — NumPy Init & Python Review — A Crash Python Review & Initialization at NumPy lib.

06Episode#PySeries — Python — NumPy Arrays & Jupyter Notebook — Arithmetic Operations, Indexing & Slicing, and Conditional Selection w/ np arrays.

07Episode#PySeries — Python — Pandas — Intro & Series — What it is? How to use it?

08Episode#PySeries — Python — Pandas DataFrames — The primary Pandas data structure! It is a dict-like container for Series objects

09Episode#PySeries — Python — Python 4 Engineers — Even More Exercises! — More Practicing Coding Questions in Python!

10Episode#PySeries — Python — Pandas — Hierarchical Index & Cross-section — Open your Colab notebook and here are the follow-up exercises!

11Episode#PySeries — Python — Pandas — Missing Data — Let’s Continue the Python Exercises — Filling & Dropping Missing Data

12Episode#PySeries — Python — Pandas — Group By — Grouping large amounts of data and compute operations on these groups

13Episode#PySeries — Python — Pandas — Merging, Joining & Concatenations — Facilities For Easily Combining Together Series or DataFrame

14Episode#PySeries — Python — Pandas — Pandas Dataframe Examples: Column Operations

15Episode#PySeries — Python —Pandas — Data Input & Output — Python In-built Functions For Creating, Writing & Reading Files(this one)

--

--

Jungletronics
Jungletronics

Published in Jungletronics

Explore our insights on Django, Python, Rails, Ruby, and more. We share code, hacks, and academic notes for developers and tech enthusiasts. Happy reading!

J3
J3

Written by J3

😎 Gilberto Oliveira Jr | 🖥️ Computer Engineer | 🐍 Python | 🧩 C | 💎 Rails | 🤖 AI & IoT | ✍️

No responses yet