2.1读取csv格式文件Reading_CSV_File

1.1 读取csv格式文件

用read_csv函数可以读取csv数据,默认数据之间是用逗号分隔开的。
有时候数据集并不是这样的啊,咱们看看比较完整的读数据参数设定。

# * coding:utf-8_*_
# 作者     :XiangLin
# 创建时间 :08/02/2020 12:52
# 文件     :2-1_Reading_CSV_File.py
# IDE      :PyCharm
# Render our plots inline
import pandas as pd
import matplotlib.pyplot as plt
plt.style.use("bmh")
plt.rc('font', family='SimHei', size=13) #显示中文
pd.set_option('display.max_columns',1000)
pd.set_option('display.width', 1000)
pd.set_option('display.max_colwidth',1000)
broken_df = pd.read_csv('bikes.csv')
# 看看前3行
print(broken_df[:3])
输出:
 Date;Berri 1;Br閎euf (donn閑s non disponibles);C魌e-Sainte-Catherine;Maisonneuve 1;Maisonneuve 2;du Parc;Pierre-Dupuy;Rachel1;St-Urbain (donn閑s non disponibles)
0                                                                                                                              01/01/2012;35;;0;38;51;26;10;16;
1                                                                                                                              02/01/2012;83;;1;68;153;53;6;43;
2                                                                                                                            03/01/2012;135;;2;104;248;89;3;58;

一脸懵逼了,读得完全不对~~ read_csv有相应的参数需要我们设定一下,比如对于上面的数据,我们希望:

数据分隔符设定为;
编码格式设定为 ‘latin1’ (默认 ‘utf8’)
解析一下 ‘Date’ 字段
按照 ‘Date’ 字段(日期)排序

fixed_df = pd.read_csv('bikes.csv',sep=';',encoding='latin1',parse_dates=['Date'],dayfirst=True,index_col='Date')
print(fixed_df[:3])

输出:
   Berri 1  Bré–Žeuf (donné–‘s non disponibles)  Cé­Œe-Sainte-Catherine  Maisonneuve 1  Maisonneuve 2  du Parc  Pierre-Dupuy  Rachel1  St-Urbain (donné–‘s non disponibles)
Date                                                                                                                                                                                
2012-01-01       35                                  NaN                       0             38             51       26            10       16                                   NaN
2012-01-02       83                                  NaN                       1             68            153       53             6       43                                   NaN
2012-01-03      135                                  NaN                       2            104            248       89             3       58         

1.2 筛选某些列

用pandas读取完CSV文件呢, 我们会得到一个叫做DataFrame的对象,由行和列组成类似上面的excel(说废话的感觉)。可以像下面这样通过列名取出其中的某一列。

print(fixed_df['Berri 1'])
输出:
Date
2012-01-01      35
2012-01-02      83
2012-01-03     135
2012-01-04     144
2012-01-05     197
2012-01-06     146
2012-01-07      98
2012-01-08      95
2012-01-09     244
2012-01-10     397
2012-01-11     273
2012-01-12     157
2012-01-13      75
2012-01-14      32
2012-01-15      54
2012-01-16     168
2012-01-17     155
2012-01-18     139
2012-01-19     191
2012-01-20     161
2012-01-21      53
2012-01-22      71
2012-01-23     210
2012-01-24     299
2012-01-25     334
2012-01-26     306
2012-01-27      91
2012-01-28      80
2012-01-29      87
2012-01-30     219
              ... 
2012-10-07    1580
2012-10-08    1854
2012-10-09    4787
2012-10-10    3115
2012-10-11    3746
2012-10-12    3169
2012-10-13    1783
2012-10-14     587
2012-10-15    3292
2012-10-16    3739
2012-10-17    4098
2012-10-18    4671
2012-10-19    1313
2012-10-20    2011
2012-10-21    1277
2012-10-22    3650
2012-10-23    4177
2012-10-24    3744
2012-10-25    3735
2012-10-26    4290
2012-10-27    1857
2012-10-28    1310
2012-10-29    2919
2012-10-30    2887
2012-10-31    2634
2012-11-01    2405
2012-11-02    1582
2012-11-03     844
2012-11-04     966
2012-11-05    2247
Name: Berri 1, Length: 310, dtype: int64

1.3 画一下某一列?

直接在这一列尾部加上 .plot() 就可以了,就喜欢这么简单粗暴!!

比如按照时间绘个图,然后发现1,2,3月骑自行车的人最多了。

print(fixed_df.head())
fixed_df['Berri 1'].plot()
plt.show()

在这里插入图片描述
你问我如果想画多列怎么办? 就像下面这样就可以啦,你告诉我它,“我需要画整个dataframe的图!!!”,然后它就画了,恩。

fixed_df.plot(figsize=(15, 10))
plt.show()

在这里插入图片描述

1.4 一句话串读取和绘图

好吧,其实我的意思就是你可以直接一口气读完数据然后画出来,恩:

df = pd.read_csv('bikes.csv',sep=';',encoding='latin1',parse_dates=['Date'],dayfirst=True,index_col='Date')
df['Berri 1'].plot()
plt.show()

在这里插入图片描述
数据链接:链接:https://pan.baidu.com/s/1Mt3sCUeJGkTs8wefOdilpQ
提取码:ysjc
来自七月在线数据挖掘算法
向林
2020年2月8日于重庆城口
好好学习,天天向上,终有所获

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值