DataFrame读取CSV文件

 读取csv的代码:

print pd.read_csv("ex1.csv")

print "\n"
print "Can also use read table with a specific separator"
print pd.read_table("ex1.csv",sep=',')

print "\n"
print "Read a csv and define a row as its header"
print pd.read_csv("ex2.csv",header=None)
print "###"
print pd.read_csv("ex2.csv",header=1)


print "\n"
print 'Set names for the columns'
name=['wang','li','zhao','qian','sun']
print pd.read_csv('ex2.csv',names=name)

print "\n"
print 'Set names for the columns and user a column as the index'
print pd.read_csv('ex2.csv',names=name,index_col='sun')

 

读取不规则csv到pandas

b = list(open('ex3.txt'))
print b
print 'User regex to split, s represents space,tap,new line, and so forth'
result = pd.read_table('ex3.txt',sep="\s+")
print result

print '\n'
print 'Skip rows from reading'
result1 = pd.read_table('ex3.txt',sep="\s+",skiprows=[1,3])
print result1

print '\n'
print 'We found some NaN somewhere'
csv5 = pd.read_csv('ex5.csv')
print csv5
print pd.isnull(csv5)

print '\n'
print "We treat 'foo' as a NaN"
csv5_fill = pd.read_csv('ex5.csv',na_values=["foo"])
print csv5_fill 

 

posted @ 2018-01-22 09:36  Junfei_Wang  阅读(14921)  评论(0编辑  收藏  举报