The CData Python Connector for CSV enables you use pandas and other modules to analyze and visualize live CSV data in Python.
The rich ecosystem of Python modules lets you get to work quickly and integrate your systems more effectively. With the CData Python Connector for CSV, the pandas & Matplotlib modules, and the SQLAlchemy toolkit, you can build CSV-connected Python applications and scripts for visualizing CSV data. This article shows how to use the pandas, SQLAlchemy, and Matplotlib built-in functions to connect to CSV data, execute queries, and visualize the results.
With built-in optimized data processing, the CData Python Connector offers unmatched performance for interacting with live CSV data in Python. When you issue complex SQL queries from CSV, the driver pushes supported SQL operations, like filters and aggregations, directly to CSV and utilizes the embedded SQL engine to process unsupported operations client-side [often SQL functions and JOIN operations].
Connecting to CSV Data
Connecting to CSV data looks just like connecting to any relational data source. Create a connection string using the required connection properties. For this article, you will pass the connection string as a parameter to the create_engine function.
The DataSource property must be set to a valid local folder name.
Also, specify the IncludeFiles property to work with text files having extensions that differ from .csv, .tab, or .txt. Specify multiple file extensions in a comma-separated list. You can also set Extended Properties compatible with the Microsoft Jet OLE DB 4.0 driver. Alternatively, you can provide the format of text files in a Schema.ini file.
Set UseRowNumbers to true if you are deleting or updating in CSV. This will create a new column with the name RowNumber which will be used as key for that table.
Follow the procedure below to install the required modules and start accessing CSV through Python objects.
Install Required Modules
Use the pip utility to install the pandas & Matplotlib modules and the SQLAlchemy toolkit:
pip install pandas pip install matplotlib pip install sqlalchemy
Be sure to import the module with the following:
import pandas import matplotlib.pyplot as plt from sqlalchemy import create_engine
Visualize CSV Data in Python
You can now connect with a connection string. Use the create_engine function to create an Engine for working with CSV data.
engine = create_engine["csv:///?DataSource=MyCSVFilesFolder"]
Execute SQL to CSV
Use the read_sql function from pandas to execute any SQL statement and store the resultset in a DataFrame.
df = pandas.read_sql["SELECT City, TotalDue FROM Customer WHERE FirstName = 'Bob'", engine]
Visualize CSV Data
With the query results stored in a DataFrame, use the plot function to build a chart to display the CSV data. The show method displays the chart in a new window.
df.plot[kind="bar", x="City", y="TotalDue"] plt.show[]
Free Trial & More Information
Download a free, 30-day trial of the CSV Python Connector to start building Python apps and scripts with connectivity to CSV data. Reach out to our Support Team if you have any questions.
Full Source Code
import pandas import matplotlib.pyplot as plt from sqlalchemy import create_engin engine = create_engine["csv:///?DataSource=MyCSVFilesFolder"] df = pandas.read_sql["SELECT City, TotalDue FROM Customer WHERE FirstName = 'Bob'", engine] df.plot[kind="bar", x="City", y="TotalDue"] plt.show[]
CSV or comma-delimited-values is a very popular format for storing structured data. In this tutorial, we will see how to plot beautiful graphs using csv data, and Pandas. We will learn how to import csv data from an external source [a url], and plot it using Plotly and pandas.
First we import the data and look at it.
In [1]:
import pandas as pd df = pd.read_csv['//raw.githubusercontent.com/plotly/datasets/master/2014_apple_stock.csv'] df.head[]
Out[1]:
2014-01-02 | 77.445395 |
2014-01-03 | 77.045575 |
2014-01-06 | 74.896972 |
2014-01-07 | 75.856461 |
2014-01-08 | 75.091947 |
Plot from CSV with Plotly Express¶
In [2]:
import pandas as pd import plotly.express as px df = pd.read_csv['//raw.githubusercontent.com/plotly/datasets/master/2014_apple_stock.csv'] fig = px.line[df, x = 'AAPL_x', y = 'AAPL_y', title='Apple Share Prices over time [2014]'] fig.show[]
Plot from CSV in Dash¶
Dash is the best way to build analytical apps in Python using Plotly figures. To run the app below, run pip install dash
, click "Download" to get the code and run python app.py
.
Get started with the official Dash docs and learn how to effortlessly style & deploy apps like this with Dash Enterprise.
Plot from CSV with graph_objects
¶
In [4]:
import pandas as pd import plotly.graph_objects as go df = pd.read_csv['//raw.githubusercontent.com/plotly/datasets/master/2014_apple_stock.csv'] fig = go.Figure[go.Scatter[x = df['AAPL_x'], y = df['AAPL_y'], name='Share Prices [in USD]']] fig.update_layout[title='Apple Share Prices over time [2014]', plot_bgcolor='rgb[230, 230,230]', showlegend=True] fig.show[]
What About Dash?¶
Dash is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library.
Learn about how to install Dash at //dash.plot.ly/installation.
Everywhere in this page that you
see fig.show[]
, you can display the same figure in a Dash application by passing it to the figure
argument of the Graph
component from the built-in dash_core_components
package like this:
import plotly.graph_objects as go # or plotly.express as px fig = go.Figure[] # or any Plotly Express function e.g. px.bar[...] # fig.add_trace[ ... ] # fig.update_layout[ ... ] import dash import dash_core_components as dcc import dash_html_components as html app = dash.Dash[] app.layout = html.Div[[ dcc.Graph[figure=fig] ]] app.run_server[debug=True, use_reloader=False] # Turn off reloader if inside Jupyter