Python 如何可视化未使用pandas读取的数据集
我有一个csv文件,并试图在不使用熊猫的情况下读取它Python 如何可视化未使用pandas读取的数据集,python,dataframe,csv,matplotlib,Python,Dataframe,Csv,Matplotlib,我有一个csv文件,并试图在不使用熊猫的情况下读取它 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
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()
)。我也需要在上面进行绘图操作
在控制台中键入此\u文件时,将给出以下输出:
`<_csv.reader at 0x7f61b2786e48>`
``
使用的示例:
通过使用索引号和拼接,您可以简单地读取前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']
0.0 8.4
更新:
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))
输出:
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']
0.0 8.4
这当然对我有帮助,但是你能看看下面的数据吗:当我使用你的代码处理这些数据时,出于某种原因,所有的东西都转换成字符串。我需要找到相应列的最小值和最大值。我还在为第0列中的值苦苦挣扎。由于某种原因,我似乎无法将其转换为int或datetime格式。有什么想法吗?是的,当然。我可以帮忙。请接受此回答,因为您原来的问题已得到回答,请打开另一个问题。当然。请参考以下链接: