首页 > 解决方案 > 如何可视化未使用 pandas 读取的数据集

问题描述

我有一个 csv 文件并尝试在不使用 pandas 的情况下读取它

import csv
data='rainfall-last-year.csv'
temporal=''
amt=''
with open(data,'r') as this_file:
  this_file=csv.reader(this_file)
  header=next(this_file)
  print(header)

  for line in this_file:
    temporal=line[0]
    amt=line[1]

如何可视化这些数据?(类似this_file.head())。我也需要对其执行绘图操作。

在控制台中输入this_file后,将给出以下输出:

`<_csv.reader at 0x7f61b2786e48>`

标签: pythondataframecsvmatplotlib

解决方案


使用示例:https ://gist.github.com/curran/a08a1080b88344b0c8a7#file-iris-csv

您可以使用索引号和拼接简单地读取前 10 行:

部分代码:

f=open(data).readlines()
for i in f[:10]: # Number of line to read
    print (i.rstrip())

完整代码:


import csv
data='iris.csv'
temporal=''
amt=''

f=open(data).readlines()
for i in f[:10]: # Number of line to read
    print (i.rstrip())

with open(data,'r') as this_file:
  this_file=csv.reader(this_file)
  header=next(this_file)
  print(header)

  for line in this_file:
    temporal=line[0]
    amt=line[1]

输出:

sepal_length,sepal_width,petal_length,petal_width,species
5.1,3.5,1.4,0.2,setosa
4.9,3.0,1.4,0.2,setosa
4.7,3.2,1.3,0.2,setosa
4.6,3.1,1.5,0.2,setosa
5.0,3.6,1.4,0.2,setosa
5.4,3.9,1.7,0.4,setosa
4.6,3.4,1.4,0.3,setosa
5.0,3.4,1.5,0.2,setosa
4.4,2.9,1.4,0.2,setosa
['sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'species']

更新:

import csv
data='rainfall-last-year.csv'
temporal=''
amt=''

f=open(data).readlines()[1:]
mmvals=[]
for i in f[:10]:
    vals=i.rstrip()
    mmvals.append(float(vals.split(",")[1]))
print (min(mmvals),max(mmvals))

输出:

0.0 8.4

推荐阅读