Comments (2)
根据课本使用pip install pandas 安装pandas应该是下了最新版,库里面有些东西就被弃用了
from data_science_tool_book_code.
根据作者的思路重新写的(主函数路口那注意一下,上传文本出了问题)
#--coding:utf-8--
from future import print_function
import pandas as pd
from collections import OrderedDict
from pandas import Series,DataFrame
import numpy as np
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
def read_data():
excel_content=pd.read_excel('A0201.xls',skiprows=2)
race_list=excel_content.iloc[0,:][1:][::3].tolist()
# print(excel_content.iloc[0,:][1:][::3])
# print(excel_content.head())
# race_list=(excel_content.ix[1:,::3]).values.tolist()
# print((excel_content.ix[1:,::3]).values.tolist())
# print((excel_content.ix[0:1,::3]))
# for m in race_list:
# print (m.decode('utf-8'))
# print(race_list)
# print(excel_content.iloc[:,0])
age_list=map(lambda x:str(x).replace(' ',''),excel_content.iloc[:,0][2:].tolist())
# for m in age_list:
# print(m.decode('utf-8'))
# print(age_list)
excel_content=pd.read_excel('A0201.xls',skiprows=4)
# print(excel_content)
# r_dict=OrderedDict()
# for i,j in excel_content.iloc[:,[4,5,6]].to_dict().items():
# new_v_dict=OrderedDict()
# for vi,vj in j.items():
# # print(vi,vj)
# new_v_dict[age_list[int(vi)]]=vj
# r_dict[i.split('.',1)[0]]=new_v_dict
# # print(new_v_dict)
# print(json.dumps(r_dict,ensure_ascii=False,indent=4))
def get_number(lines):
ret_dict=OrderedDict()
# print(lines.to_dict())
for k,v in lines.to_dict('dict').items():
new_v_dict=OrderedDict()
for vk,vv in v.items():
new_v_dict[age_list[int(vk)]]=vv
ret_dict[k.split('.',1)[0]]=new_v_dict
return ret_dict
result_dict=OrderedDict()
for i,x in enumerate(range(1,178,3)):
ids=[x,x+1,x+2]
# print(ids)
race_list[i]=race_list[i].replace(' ','')
# print(race_list[i])
result_dict[str(race_list[i])]=get_number(excel_content.iloc[:,ids])
# print (race_list[1])
# for k,v in result_dict.items():
# print(k,v)
return result_dict
def calc_mean(d):
total=0
total_age=0
for age,count in d.items():
if age.isdigit():
total+=count #统计所累加的人口数
total_age+=int(age)*count
return total_age/float(total)
if name =='main':
import json
# print(json.dumps(read_data(),ensure_ascii=False,indent=4))
#将json数据写入到.json文件中
with open("record.json","w") as f:
f.write(json.dumps(read_data(),ensure_ascii=False,indent=4))
from data_science_tool_book_code.
Related Issues (6)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from data_science_tool_book_code.