Importing a .CSV file into Python to make scatterplots and histograms [duplicate]

冷暖自知 提交于 2021-02-07 09:51:27

问题


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:


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

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'




回答2:


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




回答3:


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


来源:https://stackoverflow.com/questions/32085531/importing-a-csv-file-into-python-to-make-scatterplots-and-histograms

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!