Python 7pandas数据处理
程序员文章站
2024-03-24 21:34:04
...
import csv
from pandas import DataFrame,Series
import pandas as pd
tmp_lst=[]
with open('exrates.csv','r',encoding='gb18030',errors='ignore')as f:
reader=csv.reader(f)
for row in reader:
tmp_lst.append(row)
df=pd.DataFrame(tmp_lst[1:],columns=tmp_lst[0])
print(df)
frame=DataFrame(df)
lista=list(frame['Country'])
listb=list(frame['Currency units per'])
for i in range (0,len(lista)):
if(lista[i]=='China'):
print('\n'+'人民币的汇率为:'+str(listb[i]))
cc=listb[i]
for i in range (0,len(lista)):
if(lista[i]=='USA'):
print('\n'+'美元的汇率为:'+str(listb[i]))
uc=listb[i]
print('\n'+'人民币和美元的兑换比率为:'+str(float(cc)/float(uc)))
minnum=float(listb[1])
maxnum=float(0)
flag1=0
flag2=0
sum=0
for i in range (0,len(listb)):
if(float(listb[i])<minnum):
minnum=float(listb[i])
flag1=i
for i in range (0,len(listb)):
if(float(listb[i])>maxnum):
maxnum=float(listb[i])
flag2=i
print('汇率最高的国家是:'+str(lista[flag2])+",汇率是"+str(listb[flag2])+'\n')
print('汇率最低的国家是:'+str(lista[flag1])+",汇率是"+str(listb[flag1])+'\n')
for i in range (0,len(listb)):
sum+=float(listb[i])
average=sum/len(listb)
print("汇率均值为:"+str(average)+'\n')
print(frame.loc[frame['Currency units per']<uc])