1. DATA INPUTS IN PANDAS
Pandas provides a variety of functions to read data from files of different formats. Here are some common file formats and the corresponding functions in pandas to read them:
a. CSV (Comma-Separated Values):
– Use `pd.read_csv()` function to read data from CSV files.
“`python
import pandas as pd
# Read data from a CSV file into a DataFrame
df = pd.read_csv(‘data.csv’)
“`
b. Excel:
– Use `pd.read_excel()` function to read data from Excel files.
“`python
import pandas as pd
# Read data from an Excel file into a DataFrame
df = pd.read_excel(‘data.xlsx’, sheet_name=’Sheet1′)
“`
c. JSON (JavaScript Object Notation):
– Use `pd.read_json()` function to read data from JSON files.
“`python
import pandas as pd
# Read data from a JSON file into a DataFrame
df = pd.read_json(‘data.json’)
“`
d. SQL (Structured Query Language) Database:
– Use `pd.read_sql()` function to read data from SQL databases.
“`python
import pandas as pd
from sqlalchemy import create_engine
# Create a database connection
engine = create_engine(‘sqlite:///data.db’)
# Read data from a SQL database into a DataFrame
df = pd.read_sql(‘SELECT * FROM table_name’, engine)
“`
e. HTML (HyperText Markup Language):
– Use `pd.read_html()` function to read data from HTML tables.
“`python
import pandas as pd
# Read data from an HTML file or URL into a list of DataFrames
dfs = pd.read_html(‘data.html’)
“`
These are just a few examples of how to read data from different file formats using pandas. Depending on your data source, pandas provides a wide range of functions to efficiently import and manipulate your data.