Pandas 之入门
教程链接:https://blog.csdn.net/toto1297488504/category_9731964.html
一、十分钟入门
1.读取文件txt,转换成数组
with open("D:/PyProjects/demo/com/njbdqn/pandas/city.txt",'r',encoding='gbk') as f: # text 转换成数组 str = f.readline() a = str.strip("[]").replace("'","").split(",")
读取文件csv,分组统计
str = pd.read_csv("D:/PyProjects/demo/com/njbdqn/pandas/jobs_csv.csv") # 分组统计 print(str['location'].value_counts())
北京 48
北京-海淀区 9
北京-朝阳区 3
2.用数组组装Series
# Series(数组) => 下标是 0-1 的一维数组 citySeries = pd.Series(a)
3.组装DF,不写index,则默认0,1,2,3 ...
# DataFrame(Series,columns,index) => 二维数组,index必须是index citytab = pd.DataFrame(citySeries, columns=list('C'))
4.用DF获得Series
# Return a Series containing counts of unique rows in the DataFrame. print(citytab.value_counts()) # value出现的counts
c = citySeries.value_counts(ascending=True) # 排序
5.Series的mean
print(c.mean())
6.Series的前5个切片
print(citySeries.value_counts()[:5])