Getting Started with Pandas

Importing Pandas

import pandas as pd

Creating Series and DataFrames

Syntax for Creating a Series

    pd.Series(data, index=None, dtype=None, name=None)
    

Example

series = pd.Series([10, 20, 30, 40, 50])

Syntax for Creating a DataFrame

pd.DataFrame(data, index=None, columns=None, dtype=None, copy=False)

Example:

df = pd.DataFrame({
    'Name': ['Rohit', 'Shreya', 'Aman'],
    'Age': [21, 24, 27],
    'City': ['Mumbai', 'Delhi', 'Lucknow']
})

Loading Data from Files (CSV, Excel, JSON, etc.)

Pandas makes it easy to load data from various file formats. Let's explore how to load data from CSV, Excel, and JSON files.

Loading Data from a CSV File

import pandas as pd
# Loading data from a CSV file
df = pd.read_csv('Copy the path of your csv file here(ex-path.csv)')

Loading Data from an Excel File

import pandas as pd
# Loading data from an Excel file
df = pd.read_excel('Copy the path of your excel file here(ex-abc.xlsx)')

Loading Data from a JSON File

import pandas as pd
# Loading data from a JSON file
df = pd.read_json('Copy the path of your json file here(ex-abc.json)')

Exporting Data to Files

Pandas allows you to easily export your DataFrame to various file formats. Here is how you can export data to CSV, Excel, and JSON files.

Exporting Data to a CSV File

import pandas as pd
# Exporting DataFrame to a CSV file
df.to_csv('output.csv', index=False)

Exporting Data to an Excel File

import pandas as pd
# Exporting DataFrame to an Excel file
df.to_excel('output.xlsx', index=False)

Exporting Data to a JSON File

import pandas as pd
# Exporting DataFrame to a JSON file
df.to_json('output.json', orient='records')

As you can see to export data from a Pandas DataFrame to different file formats like CSV, Excel, and JSON, you can use the to_csv(), to_excel(), and to_json() methods, respectively.