3

I am trying to import a .CSV file (converted from an Excel file) into Python so I would be able to make correlation/scatter plots and histograms.

How do I do that?

1
  • 2
    Ideally you need to show what you've tried/researched already. What have you looked at? Commented Aug 19, 2015 at 2:35

3 Answers 3

14

While you can use the csv module if you need to work with a csv file line by line, the pandas and matplotlib modules provide a higher level interface for data analysis tasks.

data.csv

x,y
1,2
2,4
3,6
4,7
5,11
6,12
7,13
8,20
9,17
10,19

plots.py

import pandas as pd
import matplotlib.pyplot as plt
df  = pd.read_csv("data.csv")
df.plot()  # plots all columns against index
df.plot(kind='scatter',x='x',y='y') # scatter plot
df.plot(kind='density')  # estimate density function
# df.plot(kind='hist')  # histogram

output

enter image description here

How it Works

df  = pd.read_csv("data.csv")

read_csv() reads the csv file into a Pandas Dataframe

The dataframe plot method is a wrapper around matplotlib's plot and is documented here

Notice that we can get different kind of plots by adjusting the kind= keyword parameter to df.plot(). Histograms are available, in a newer version of matplotlib than is installed here, with kind='hist'

Sign up to request clarification or add additional context in comments.

Comments

2

Python has built in support for csv files: https://docs.python.org/2/library/csv.html. There are several examples in the documentation.

Comments

0

First import csv, then you can use this code to open your csv file. With the line for row in reader: you can loop through the rows in your csv file and use your code do whatever you need to do.

import csv
with open('your_file.csv', 'rb') as f:
    reader = csv.reader(f)
    for row in reader:
        #do something

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.