PYTHON笔记第九章

import pandas as pd#导入熊猫库
datas=[[65,92,78,83,70],[90,72,76,93,56],[81,85,91,89,77],[79,53,47,94,80]]#数据
indexs=["aaa","bbb","ccc","ddd"]#定义索引,即横坐标属性
columns=["Chi","Mat","Eng","Nat","Soc"]#定义列坐杯属性
df=pd.DataFrame(datas,columns=columns,index=indexs)#导入数据,横纵坐标属性
print(df.head(2))#打印头两行
print(df.tail(2))#打印尾两行

import pandas as pd
datas=[[65,92,78,83,70],[90,72,76,93,56],[81,85,91,89,77],[79,53,47,94,80]]
indexs=["aaa","bbb","ccc","ddd"]#横坐标
columns=["Chi","Mat","Eng","Nat","Soc"]#纵坐标
df=pd.DataFrame(datas,columns=columns,index=indexs)#建表
print(df.loc["ddd",:])#输出DDD行,所有列值,注意这里会打竖输出末尾加属性名与种类
print(df.loc["ddd"]["Mat"])#输出DDD行MAT列值
print(df.loc[("ddd","ccc"),:])#输出DDD,CCC行值53
print(df.loc[("ddd","ccc"),("Chi","Mat")])#输出DDD,CCC行,CHI,MAT列值
print(df.loc["ccc":"ddd","Chi":"Eng"])#输出CCC到DDD行,CHI到ENG列值
print(df.loc[:"ddd","Chi":"Eng"])#输出第0列到DDD行,CHI到ENG列值
print(df.loc["ccc":,"Chi":])#输出CCC到末行,CHI到末列值

import pandas as pd
datas=[[65,92,78,83,70],[90,72,76,93,56],[81,85,91,89,77],[79,53,47,94,80]]
indexs=["aaa","bbb","ccc","ddd"]
columns=["Chi","Mat","Eng","Nat","Soc"]
df=pd.DataFrame(datas,columns=columns,index=indexs)
print(df)
indexs[0]="eee"
df.index=indexs
columns[3]="Phy"
df.columns=columns
print(df)
print(df[df.Mat>=80])#输出数学列大于80的列
print(df[["Mat"]])#输出数学列
print(df[["Chi","Eng"]])#输出语文,英语列
print(df.values)#输出全部值
print(df.values[1])#输出第1行
print(df.values[1][2])#输出第1行第2列

import pandas as pd
datas=[[65,92,78,83,70],[90,72,76,93,56],[81,85,91,89,77],[79,53,47,94,80]]
indexs=["aaa","bbb","ccc","ddd"]
columns=["Chi","Mat","Eng","Nat","Soc"]
df=pd.DataFrame(datas,columns=columns,index=indexs)
print(df)
indexs[0]="eee"#改行属性
df.index=indexs#赋值
columns[3]="Phy"#改列属性
df.columns=columns#赋值
print(df)#输出

import pandas as pd
datas=[[65,92,78,83,70],[90,72,76,93,56],[81,85,91,89,77],[79,53,47,94,80]]
indexs=["aaa","bbb","ccc","ddd"]
columns=["Chi","Mat","Eng","Nat","Soc"]
df=pd.DataFrame(datas,columns=columns,index=indexs)
print(df.iloc[0][0])#65,第0行第0列元素
print(df.ix["ccc"][2])#91,CCC行第2列元素

import pandas as pd
datas=[[65,92,78,83,70],[90,72,76,93,56],[81,85,91,89,77],[79,53,47,94,80]]
indexs=["aaa","bbb","ccc","ddd"]
columns=["Chi","Mat","Eng","Nat","Soc"]
df=pd.DataFrame(datas,columns=columns,index=indexs)
print(df)#输出
indexs[0]="eee"#修改
df.index=indexs#赋值,修改第0行属性那个列表
columns[3]="Phy"#第2列属性
df.columns=columns#赋值,修改第2列属性那个列表
print(df)#输出
print(df[df.Mat>=80])#数学成绩大过80的列
print(df[["Mat"]])#数学成绩列
print(df[["Chi","Eng"]])#语文,英语成绩列

import pandas as pd
datas=[[65,92,78,83,70],[90,72,76,93,56],[81,85,91,89,77],[79,53,47,94,80]]
indexs=["aaa","bbb","ccc","ddd"]
columns=["Chi","Mat","Eng","Nat","Soc"]
df=pd.DataFrame(datas,columns=columns,index=indexs)
print(df.iloc[0][0])#第0行第0列
'''
loc:通过行标签索引数据
iloc:通过行号索引行数据
ix:通过行标签或行号索引数据(基于loc和iloc的混合)
'''

import pandas as pd
datas=[[65,92,78,83,70],[90,72,76,93,56],[81,85,91,89,77],[79,53,47,94,80]]
indexs=["aaa","bbb","ccc","ddd"]
columns=["Chi","Mat","Eng","Nat","Soc"]
df=pd.DataFrame(datas,columns=columns,index=indexs)#生成表格
df1=df.sort_values(by="Mat",ascending=False)#按MAT排序,降序
print(df1)#输出

import pandas as pd
tables=pd.read_html("http://value500.com/M2GDP.html")
n=1
for table in tables:
    print("no.{}".format(str(n)))#提示:第N行
    print(table.head())#解释了为何是第19个图表
    print()#换行
    n+=1#行数加1
    
import pandas as pd
dt=pd.read_html("http://www.86pm25.com/city/beijing.html")
data=dt[0]#读出第0个表格
print(data)#输出
print()#换行
print(dt)#输出所有表格,输出可见有个[]表示列表
import pandas as pd
datas=[[65,92,78,83,70],[90,72,76,93,56],[81,85,91,89,77],[79,53,47,94,80]]
indexs=["aaa","bbb","ccc","ddd"]
columns=["Chi","Mat","Eng","Nat","Soc"]
df=pd.DataFrame(datas,columns=columns,index=indexs)
df.ix[1][1]=999#改值
df.ix[3][3]=-22#改值
print(df)

import pandas as pd
from pylab import *
datas=[[65,92,78,83,70],[90,72,76,93,56],[81,85,91,89,77],[79,53,47,94,80]]
indexs=["林大明","陈聪明","黄美丽","熊小娟"]
columns=["语文","数学","英文","自然","社会"]
df=pd.DataFrame(datas,columns=columns,index=indexs)#生成表格
df.plot()#就是这句自带画图功能!!!呵呵,简单
import pandas as pd
datas=[[65,92,78,83,70],[90,72,76,93,56],[81,85,91,89,77],[79,53,47,94,80]]#数据
indexs=["aaa","bbb","ccc","ddd"]#定义行属性
columns=["Chi","Mat","Eng","Nat","Soc"]#定义列属性
df=pd.DataFrame(datas,columns=columns,index=indexs)#生成表格

df1=df.drop("ccc")#删掉属性为CCC的行
print(df1)
df2=df.drop("Mat",axis=1)#删列时要设为1,删行时因为默认为0所以可以省略
print(df2)
df3=df.drop(["Eng","Nat"],axis=1)#删除列为ENG与NAT的列
print(df3)
print("")
df4=df.drop(df.index[2:3])#删除下标为2的行
print(df4)
df5=df.drop(df.columns[2:3],axis=1)#除于下标为2的列
print(df5)
import pandas as pd
tables=pd.read_html("http://value500.com/M2GDP.html")#读取网页中的所有表格
print(tables)#掂过碌蔗
print()
print()
print()
print()
print()
table=tables[18]#读出第18个表格
table=table.drop(table.index[[0,1]])#删掉标题
table.columns=["year","M2","GDP","M2/GDP"]#自设标题改变列属性
table.index=range(len(table.index))#以table行数即0-(行MAX-1)重新编号
print(table)#输出表格
import pandas as pd
dp=pd.DataFrame({"aaa":[65,92,78,83,70],"bbb":[90,72,76,93,56],
                 "ccc":[81,85,91,89,77],"ddd":[79,53,47,94,80]})
print (dp)#直接打印
import pandas as pd

datas=[[65,92,78,83,70],[90,72,76,93,56],[81,85,91,89,77],[79,53,47,94,80]]
indexs=["aaa","bbb","ccc","ddd"]
columns=["Chi","Mat","Eng","Nat","Soc"]
df=pd.DataFrame(datas,columns=columns,index=indexs)
print(df)#打印全部表格

你可能感兴趣的:(PYTHON笔记第九章)