python - 如何可视化未使用 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>`
解决方案
使用示例: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
推荐阅读
- c - c语言函数调用中的参数太多?
- c++ - 如何通过在 C++ 中接收 std::istream 对象来读取和解析以逗号分隔的用户的输入?
- arrays - 如何修复 PHP 中的数组错误未定义偏移量
- optimization - 如果给予更多限制,Ortools/gurobi 会运行得更快吗?
- ruby-on-rails - Rails - 枚举未包含在列表中
- python - 如何让我的方法返回一个列表而不是字符串?
- python - 尝试在 Python 中将时间戳转换为毫秒。出现格式错误
- python - 未创建类
- powershell - 如何完成包装功能?
- python - 如何让我的不和谐音乐机器人识别一首歌曲播放完毕,以便播放下一首歌曲?