读写文本的数据格式
import pandas as pd
#1.read_csv是以逗号分隔,将其读入一个DataFrame:
file=open("E:\李明霞\利用python数据分析的源代码\ch06\ex1.csv")
df1=pd.read_csv(file)
print(df1)
#2.read_table读取,需要指定分隔符
df2=pd.read_table(file,sep=",")
print(df2)
answer:
a b c d message
0 1 2 3 4 hello
1 5 6 7 8 world
2 9 10 11 12 foo
#3.如果没有列名,可以自动获取或者自定义列名
file2=open("E:\李明霞\利用python数据分析的源代码\ch06\ex2.csv")
df3=pd.read_csv(file2)
df4=pd.read_csv(file2,header=None)
print(df3)
print(df4)
answer:
1 2 3 4 hello
0 5 6 7 8 world
1 9 10 11 12 foo
#这样列表就会自动将内容中的第一行作为列表标头,会造成错区,因此我们可以将标头header设置成none
0 1 2 3 4
0 1 2 3 4 hello
1 5 6 7 8 world
2 9 10 11 12 foo
#自动获取后
#4.定义索引
names=['a','b','c','d','message']
df4=pd.read_csv(file2,names=names)
df5=pd.read_csv(file2,names=names,index_col="message")
print(df4)
print(df5)
df4:
a b c d message
0 1 2 3 4 hello
1 5 6 7 8 world
2 9 10 11 12 foo
df5:
a b c d
message
hello 1 2 3 4
world 5 6 7 8
foo 9 10 11 12
#将message列做成DataFrame的索引
#5层次索引
#将多个列做成一个层次化索引,只需传入由列编号或列名组成的列表即可:
file3=open("E:\李明霞\利用python数据分析的源代码\ch06\csv_mindex.csv")
df6=pd.read_csv(file3,index_col=["key1","key2"])
print(df6)
answer:
value1 value2
key1 key2
one a 1 2
b 3 4
c 5 6
d 7 8
two a 9 10
b 11 12
c 13 14
d 15 16
#6.txt文本格式里面含有多样空格,利用正则表达式\s(匹配任意空白字符)
file4=open("E:\李明霞\利用python数据分析的源代码\ch06\ex3.txt")
df7=pd.read_table(file4,sep="\s+")
print(df7)
A B C
aaa -0.264438 -1.026059 -0.619500
bbb 0.927272 0.302904 -0.032399
ccc -0.264273 -0.386314 -0.217601
ddd -0.871858 -0.348382 1.100491
#7.数据清洗中去除数据行,跳跃去除某些行数据,比如乱码行和缺失值行
#skiprows=[] 里面填写跳跃的数据行索引
file5=open("E:\李明霞\利用python数据分析的源代码\ch06\ex4.csv")
df8=pd.read_csv(file5)
df9=pd.read_csv(file5,skiprows=[0,2,3])
print(df8)
print(df9)
df8:
# hey!
a b c d message
# just wanted to make things more difficult for... NaN NaN NaN NaN
# who reads CSV files with computers anyway? NaN NaN NaN
1 2 3 4 hello
5 6 7 8 world
9 10 11 12 foo
df9:
a b c d message
0 1 2 3 4 hello
1 5 6 7 8 world
2 9 10 11 12 foo
#8.指定数组中的某个值设定为空值:na_values()
#na_values可以用一个列表或集合的字符串表示缺失值
file6=open("E:\李明霞\利用python数据分析的源代码\ch06\ex5.csv")
df10=pd.read_csv(file6)
print(df10)
answer:
something a b c d message
0 one 1 2 3.0 4 NaN
1 two 5 6 NaN 8 world
2 three 9 10 11.0 12 foo
#列表里的某些值设定为空值
df10=pd.read_csv(file6,na_values=['world',"two"])
something a b c d message
0 one 1 2 3.0 4 NaN
1 NaN 5 6 NaN 8 NaN
2 three 9 10 11.0 12 foo
#集合里面的值设定为空值
sentinels={"message":["foo","NA"],"something":["two"]}
df10=pd.read_csv(file6,na_values=sentinels)
something a b c d message
0 one 1 2 3.0 4 NaN
1 NaN 5 6 NaN 8 world
2 three 9 10 11.0 12 NaN
逐块读取文本文件
#在看大文件之前,我们先设置pandas显示地更紧些:
pd.options.display.max_rows=10
file7=open("E:\李明霞\利用python数据分析的源代码\ch06\ex6.csv")
#1.只想读取几行(避免读取整个文件),通过nrows进行指定即可
df11=pd.read_csv(file7,nrows=10)
print(df11)
#2.逐块读取文件,可以指定chunksize(行数)
df12=pd.read_csv(file7,chunksize=1000)
print(df12)
<pandas.io.parsers.TextParser at 0x8398150>
#3.值聚合
read_csv所返回的这个TextParser对象使你可以根据chunksize对文件进行逐块迭代。比如说,我们可以迭代处理ex6.csv,将值计数聚合到"key"列中,如下所示:
#空一维数组
tot=pd.Series([])
#对df12里面的每一条进行迭代,索引key列名下的东西,并进行计数,对没有的数据可以补充值为0
for piece in df12:
tot=tot.add(piece["key"].value_counts(),fill_value=0)
#对tot进行降序排序,默认是升序,ascending=Flase为降序
tot=tot.sort_values(ascending=False)
#因为数据太多,所以可以用一维数组索引方式获取前10个数据
print(tot[:10])
answer:
E 368.0
X 364.0
L 346.0
O 343.0
Q 340.0
M 338.0
J 337.0
F 335.0
K 334.0
H 330.0
dtype: float64
将数据写出到文本格式