pandas cookbook【1】

本文分享了使用Pandas库读取CSV文件的经验,并详细解释了read_csv函数的参数设置,如分隔符、编码及日期解析等。同时,记录了一个在特定版本组合下遇到的绘图bug及其解决方案。

网上看到了关于pandas的用法,虽然练习了不少,但是还是有些不是能记得很清楚。所以就写下来了。

chapter1讲的是读取CSV文件。如下代码:

 1 #%%
 2 import pandas as pd
 3 import numpy as np
 4 import matplotlib.pyplot as plt
 5 # make the graphs a bit prettier
 6 pd.set_option('display.mpl_style','default')
 7 plt.rcParams['figure.figsize'] = (15,5)
 8 
 9 #%%
10 broken_df = pd.read_csv('C:\Users\rui\Desktop\pandas-cookbook-master\data\bikes.csv')
11 #look at the first 3rows;

其中,关于read_csv的用法,还没有进行深入的了解。之后下一篇会做一个专门的讲解。

1 fixed_df = pd.read_csv('C:\Users\rui\Desktop\pandas-cookbook-master\data\bikes.csv',
2                        sep=';',encoding='latinl',parse_dates=['Date'],dayfirst=True,index_col='Date')

其中,sep代表分隔符,encoding是指明编码,如果文件中包含非-ASCII字符字段,要确保以正确的编码方式读取。这是在UTF-8的本地系统里面读取Latin-1文件的一个主要问题。此时,可以如下处理。对参数parse_date:if True then index will be parsed as dates (False by default). You can specify more complicated options to parse a subset of columns or a combination of columns into a single date column (list of ints or names, list of lists, or dict) [1, 2,3];

dayfirst:boolean, default False //DD/MM format dates, international and European format;

index_col:int or sequence or False, default None

   Column to use as the row labels of the DataFrame. If a sequence is given, a MultiIndex is used. If you have a malformed file with delimiters at the end of each           line, you might consider index_col=False to force pandas to _not_ use the first column as the index (row names)

  1 fixed_df['Berri 1'].plot() 2 fixed_df.plot(figsize=(15,5)) 

其中,plot函数并不能和原文档里一样画出Berri 1的数据。目前正在寻找原因。

经查证,是 a bug in Pandas 0.17.1 with Matplotlib 1.5.0。

做如下更改,程序完美运行:

将代码:

 1 import pandas as pd 2 pd.set_option('display.mpl_style', 'default') 

更改为:

 1 import matplotlib 2 matplotlib.style.use('ggplot') 

 

转载于:https://www.cnblogs.com/shr123/p/5754993.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值