将.CSV文件导入Python以制作散点图和直方图 [英] Importing a .CSV file into Python to make scatterplots and histograms

查看:84
本文介绍了将.CSV文件导入Python以制作散点图和直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将.CSV文件(从Excel文件转换为)导入Python,以便能够绘制相关/散点图和直方图.

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.

我该怎么做?

推荐答案

如果需要逐行处理csv文件,可以使用 csv 模块,而 pandas matplotlib 模块为数据分析任务提供了更高级别的界面.

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

输出

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

read_csv()会将csv文件读入 Pandas数据框

read_csv() reads the csv file into a Pandas Dataframe

dataframe绘图方法是对matplotlib绘图的封装,并且在此处记录

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

请注意,通过将 kind = 关键字参数调整为 df.plot(),我们可以获得不同类型的图.在比此处安装的更新版本更高的matplotlib中,可以使用直方图,其中带有 kind ='hist'

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'

这篇关于将.CSV文件导入Python以制作散点图和直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆