python读写csv文件

1用python做数据分析,数据来源一般会有多种格式,而我喜欢以CSV的方式进行存储分析,如果数据本身就是CSV格式的那我可以直接用pandas库自带的方法进行读取即可,但是有时候各种来源的数据不方便用pandas进行直接分析处理,所以就有了一个通用的读写CSV文件的方法。

#1导入相关包
import os
import re
import csv
#1读取csv文件
def read_csv(filename, header=False):
    res = []
    with open(filename) as f:
        f_csv = csv.reader(f)
        if header:#默认读取头部文件
            headers = next(f_csv)
            header = False
        for row in f_csv:
            res.append(row)
    return res
#2写入csv文件
def write_csv(data, filename):
    with open(filename, "wb") as f:
        f_csv = csv.writer(f)
        #一行一行写入
        for item in data:
            f_csv.writerow(item)

2.有时候文件是txt或者从hive等数据库导出来的格式则可以用下面的方法读取数据并进行分析

#3读取文本格式
def read_text(filename, columns, delimeter):
    #columns:多少列
    #delimeter:分隔符
    res = []
    with open(filename, "rb") as f:
        while True:
            line = f.readline()
            if line:
                line = re.sub("[\r\n]", "", line)#清楚换行符
                lines = line.split(delimeter)
                if len(lines) != columns:
                    continue
                res.append(lines)
            else:
                break
    return res

3.也可以用numpy直接进行读取文件格式:loadtxt(fname, dtype=float,delimiter=None,skiprows=0, usecols=None, unpack=False)fname:文件名,dtype:数据类型,也可使是str,delimiter:分隔符,skiprows:跳过开头几行,usecols:读取某一列或者几列的值比如(0,3)表示读取第一和四列的值。
例子:npload.txt
  40920 8.326976 0.953952 3 qwe
  14488 7.153469 1.673904 2 aad
  26052 1.441871 0.805124 1 zc
  75136 13.147394 0.428964 1 wed

import numpy as np
filename = "E:/PythonProject/CommonFunction/input/npload.txt"
res = np.loadtxt(filename,dtype=str,delimiter="\t",skiprows=1,usecols=(0,3,4),unpack=False)
print type(res)
print res
x,y,z = np.loadtxt(filename,dtype=str,delimiter="\t",skiprows=1,usecols=(0,3,4),unpack=True)
print x#第一列
print y#第四列
print z#第五列

结果:

<type 'numpy.ndarray'>
[['14488' '2' 'aad']
 ['26052' '1' 'zc']
 ['75136' '1' 'wed']]
['14488' '26052' '75136']
['2' '1' '1']
['aad' 'zc' 'wed']

4.pandas也是一个强大的数据分析工具,直接读取csv,excel文件,或者吧pandas的DataFrame直接存储为csv或者excel格式:例如把上面的数据可以通过write_csv()方法存储为csv格式,然后可以直接用pandas读取。
pd.read_csv(filename, header=None, index_col=0, usecols=(1,2,3), skiprows=0)参数和np.loadtxt()参数解释基本是一样的。read_excel(io,sheet_name=0,header=0,index_col=None,usecols=None,dtype=None,skiprows=None)常用的参数解释都是一样的。相应的存储方法则是to_csv()和to_excel()

import pandas as pd
filename="E:/PythonProject/CommonFunction/input/npload.csv"
df = pd.read_csv(filename, header=None, index_col=0, usecols=(1,2,3), skiprows=0)
print df.head()

结果:

                 2  3
1                     
8.326976   0.953952  3
7.153469   1.673904  2
1.441871   0.805124  1
13.147394  0.428964  1
5.用的着的小技巧:pandas的pivot方法和numpy的permutation
df = pd.DataFrame({'foo': ['one', 'one', 'one', 'two', 'two',
...                            'two'],
...                    'bar': ['A', 'B', 'C', 'A', 'B', 'C'],
...                    'baz': [1, 2, 3, 4, 5, 6],
...                    'zoo': ['x', 'y', 'z', 'q', 'w', 't']})
print df
df.pivot(index='foo', columns='bar', values='baz')#以foo为索引列,以bar列为行,zoo列为值,注(foo, bar)不能重复

结果

bar  baz  foo zoo
0   A    1  one   x
1   B    2  one   y
2   C    3  one   z
3   A    4  two   q
4   B    5  two   w
5   C    6  two   t
bar A B C
foo
one 1 2 3
two 4 5 6
np.random.seed(0)
print np.random.permutation(10)#将0-9随机打乱,可用于随机取数据集
x = range(10)
np.random.shuffle(x)#洗牌,参数需要是一个可迭代的对象
print x
[2 8 4 9 1 6 7 3 0 5]
[3, 5, 1, 2, 9, 8, 0, 6, 7, 4]
注:当然也可以用sklearn的train_test_split方法分割数据集
from sklearn.model_selection import train_test_split
train_set,test_set = train_test_split(SampleData, test_size=0.2, random_state=42)
如果数据集含有标签,可以和标签一块分割 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)
 
 

 

转载于:https://www.cnblogs.com/junpengwang/p/10803339.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值