处理数据1
from plotly.offline import plot
from plotly.tools import FigureFactory as FF
import pandas as pd
#整一些库进来,然而第二行"from plotly.tools import FigureFactory as FF"是由于好像版本问题,我替换了原文""
f=open("score.txt","r",encoding='utf-8')
#长啥样后面有图片
k=f.readlines()
count=len(k)
for i in range(1,count):
k[i]=k[i].split() # 把字符串搞成列表
k[i][3]=float(k[i][3])
k[i][4]=float(k[i][4])
k[i][5]=float(k[i][5]) #把str搞成float后面计算
k[0]=k[0].split() #导入表头行
data=pd.DataFrame(k[1:],columns=k[0]) #把表头和内容导入进去
data["总评成绩"]=data["笔试"]*0.5+data["平时"]*0.25+data["实验"]*0.25
table=FF.create_table(data)
plot(table,show_link=False)
运行结果:
导入的源文件(txt):
把图2弄成csv
s=open("score.txt","r",encoding="utf-8")
back=open("score_副本.txt","w",encoding="utf-8")
ll=""
for l in s:
ll+=l.replace(" ",",") #把l这个字符串里的空格用","代替,然后全写进一个字符串ll,正好换行\n没删
back.write(ll)
s.close()
back.close()
弄了一个副本csv:
读取csv处理数据2
from plotly.offline import plot
from plotly.tools import FigureFactory as FF
import pandas as pd
data=pd.read_csv("score_副本.csv",encoding="utf_8")
table=FF.create_table(data)
plot(table,show_link=False)
运行结果:
老师的代码如图(有很多不同,不会解决):
from plotly.offline import plot
from plotly.tools import FigureFactory as FF
import pandas as pd
data=pd.read_csv("score_副本.csv",encoding="utf_8")
table=FF.create_table(data)
plot(table,filename='score.html',show_link=False)#在对应路径文件夹产生了一个score.html文件
处理excel数据3
import pandas as pd
data=pd.read_csv("score_副本.csv",encoding="utf_8")
data.to_excel("score.xlsx",index=0)
data1=pd.read_excel("score.xlsx")
data1["总评"]=data1["笔试"]*0.5+data1["平时"]*0.25+data1["实验"]*0.25
data1.to_excel("score_副本.xlsx",index=0)
结果:
处理数据json4
import pandas as pd
data=pd.read_excel("score.xlsx")
data.to_json("score.json",force_ascii=False)#有中文时不用ASCII
jsondata=pd.read_json("score.json")
print(jsondata)
这里的输出多了一列???,(print)输出结果
学号 姓名 专业 笔试 平时 实验
0 SA21234015 李子豪 凝聚态物理 90 87 90
1 SA21234010 孟鑫勇 垃圾治理 80 90 90
从excel读取,并访问Dataframe
import pandas as pd
data=pd.read_excel("score.xlsx")
print(data)
print()#打印一个空行
print(data.iloc[0,2])#row1,cloumn3
print()
print(data.iloc[0:2,1:4])#不算列名那一行,第0行从内容行算起
print()
print([name for name in data])#访问表头行
输出结果:
学号 姓名 专业 笔试 平时 实验
0 SA21234015 李子豪 凝聚态物理 90 87 90
1 SA21234010 孟鑫勇 垃圾治理 80 90 90
凝聚态物理
姓名 专业 笔试
0 李子豪 凝聚态物理 90
1 孟鑫勇 垃圾治理 80
['学号', '姓名', '专业', '笔试', '平时', '实验']
pandas做数据整理
先建立一个要处理的excel,为了练习语法,我也用pandas建立
import pandas as pd
data=pd.DataFrame([["China","3700","3900"]],columns=("country","2000","2001"))
#Dataframe导入数据时候,内容必须是大列表套小列表,columns就是一个去掉[]的列表
data.to_excel("gdp.xlsx",index=0)
结果:
开始正常处理
import pandas as pd
data=pd.read_excel("gdp.xlsx")
c=[name for name in data]#由列名组成的列表
#创建空的dataframe
result=pd.DataFrame(columns=("country","year","ppp-gdp"))
for i in range(len(c)-1):
result.loc[i]=[data.iloc[0,0],c[i+1],data.iloc[0,i+1]]
#result.loc[i]表示第i行,进去随便写
print(result)
result.to_excel("gdp_处理.xlsx",index=0)
结果:
country year ppp-gdp
0 China 2000 3700
1 China 2001 3900
表格拼接
同样先再写一个和图8一样的表格
`import pandas as pd
data=pd.DataFrame([["China","2001","73.0"],["China","2000","70.3"]],\
columns=("country","year","life-exp"))
data.to_excel("life.xlsx",index=0)
结果:
进行表格拼接
import pandas as pd
data1=pd.read_excel("gdp_处理.xlsx")
data2=pd.read_excel("life.xlsx")
print(data1)
print(data2)
data3=pd.merge(data1,data2,how='left')
print(data3)
结果:
country year ppp-gdp
0 China 2000 3700
1 China 2001 3900
country year life-exp
0 China 2001 73.0
1 China 2000 70.3
country year ppp-gdp life-exp
0 China 2000 3700 70.3
1 China 2001 3900 73.0
更改一个参数:
data3=pd.merge(data1,data2,on="year",how='left')
print(data3)
输出结果:
country_x year ppp-gdp country_y life-exp
0 China 2000 3700 China 70.3
1 China 2001 3900 China 73.0
在指定拼接主键的情况下,想达到第一种输出形式,需要:
data22=data2[["year","life-exp"]]#只取两列进入后面的拼接(包含主键)
data3=pd.merge(data1,data22,on="year",how='left')
print(data3)
结果:
country year ppp-gdp life-exp
0 China 2000 3700 70.3
1 China 2001 3900 73.0
关于这个拼接函数日后看这个 - 知乎 (zhihu.com)
excel表格合并
import pandas as pd
startyear=int(input())
#人均期望寿命
dataset=pd.read_excel("life.xlsx")
a=pd.DataFrame(columns=("country","li
fe-exp","year"))
for i in range(startyear,2019):
b=dataset[['country',i]].copy()
b['year']=i
b.columns=['country','life-exp','year']
c=a.append(b)
a=c
#人均收入,以PPP计算
dataset=pd.read_excel("income.xlsx")
x=pd.DataFrame(columns=("country",'i
ncome',"year"))
for i in range(startyear,2019):
y=dataset[['country',i]].copy()
y['year']=i
y.columns=['country','income','year']
z=x.append(y)
x=z
data=pd.merge(a,x)
data.to_excel(f"{startyear}到2018人均
GDP和人均寿命.xlsx")