Skip to main content

Financial data analysis I python preview -- dictionary sorting, calculating annual growth rate

· 6 Minutes to read
Allen Ma

I recently joined a factory for an internship in an internet finance related position and found a few rhymes with what I had learned. So I decided to take the time to list a few relevant cases to learn and learn.

Case (I) python warm-up

Most of the data acquisition and processing in the project uses the python programming language, so let's first review some common functions and writing rules.

Project one: dictionary statistics sorting

The dictionary d stores the correspondence between the 42 double-class universities in China and the provinces where they are located. Please use this list as a data variable to improve the Python code and count the number of schools in each province.‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬

The output shows the provinces with the highest numbers and the quantities.‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬

d = {"北京大学":"北京", "中国人民大学":"北京","清华大学":"北京",\ "北京航空航天大学":"北京","北京理工大学":"北京","中国农业大学":"北京",\ "北京师范大学":"北京","中央民族大学":"北京","南开大学":"天津",\ "天津大学":"天津","大连理工大学":"辽宁","吉林大学":"吉林",\ "哈尔滨工业大学":"黑龙江","复旦大学":"上海", "同济大学":"上海",\ "上海交通大学":"上海","华东师范大学":"上海", "南京大学":"江苏",\ "东南大学":"江苏","浙江大学":"浙江","中国科学技术大学":"安徽",\ "厦门大学":"福建","山东大学":"山东", "中国海洋大学":"山东",\ "武汉大学":"湖北","华中科技大学":"湖北", "中南大学":"湖南",\ "中山大学":"广东","华南理工大学":"广东", "四川大学":"四川",\ "电子科技大学":"四川","重庆大学":"重庆","西安交通大学":"陕西",\ "西北工业大学":"陕西","兰州大学":"甘肃", "国防科技大学":"湖南",\ "东北大学":"辽宁","郑州大学":"河南", "湖南大学":"湖南", "云南大学":"云南", \ "西北农林科技大学":"陕西", "新疆大学":"新疆"}

输出格式 == Province: Number (Chinese colon)==

d = {"北京大学":"北京", "中国人民大学":"北京", "清华大学":"北京",
"北京航空航天大学":"北京", "北京理工大学":"北京", "中国农业大学":"北京",
"北京师范大学":"北京", "中央民族大学":"北京", "南开大学":"天津",
"天津大学":"天津", "大连理工大学":"辽宁", "吉林大学":"吉林",
"哈尔滨工业大学":"黑龙江", "复旦大学":"上海", "同济大学":"上海",
"上海交通大学":"上海","华东师范大学":"上海", "南京大学":"江苏",
"东南大学":"江苏", "浙江大学":"浙江", "中国科学技术大学":"安徽",
"厦门大学":"福建", "山东大学":"山东", "中国海洋大学":"山东",
"武汉大学":"湖北", "华中科技大学":"湖北", "中南大学":"湖南",
"中山大学":"广东", "华南理工大学":"广东", "四川大学":"四川",
"电子科技大学":"四川", "重庆大学":"重庆", "西安交通大学":"陕西",
"西北工业大学":"陕西", "兰州大学":"甘肃", "国防科技大学":"湖南",
"东北大学":"辽宁","郑州大学":"河南", "湖南大学":"湖南", "云南大学":"云南",
"西北农林科技大学":"陕西", "新疆大学":"新疆"}
counts = {}
for place in d.values():
counts[place] = counts.get(place, 0) + 1
items = list(counts.items())
items.sort(key=lambda x: x[1], reverse=True)
print(items[0][0]+':'+str(items[0][1]))

Results of the run. pg1

Project 2: Calculating the annual growth rate of mobile phone sales

  1. The file smartphone.txt holds annual sales data of mobile phones for certain companies, with each row containing a number of annual sales (in millions) for each company, with tabs as separators between the data.

  2. To open the file, please specify the file encoding format: with open("smartPhone.txt",encoding="gbk") as f:

smartPhone.txt文件内容如下:

公司  2014年   2015年   2016年   2017年
Samsung 311 322.9 310.3 318.7
Apple 192.9 231.6 215.2 15.8
Huawei 73.6 104.8 139.1 153.1
OPPO 29.9 50.1 92.9 121.1
Vivo 19.5 40.5 74.3 100.7
ZTE 43.8 56.2 60.1 44.9
LG 59.2 59.7 55.1 55.9
Lenovo 70.1 74.1 50.7 49.7
Xiaomi 61.1 70.7 61.5 96.1
  1. write function isBigGrowth(L, rate), the form of reference L for a set of numerical data containing a list (a company's sales in various years), rate for the annual growth rate, to determine and return whether the annual sales growth: if the annual sales growth rate exceeds the given rate, it is True, otherwise False.

  2. The main program reads the data in smartphone.txt, converts each line of data into numerical data, and uses the function isBigGrowth(L,rate) to calculate and screen output whether the annual sales of each company are growing rapidly (this question sets the annual sales growth rate of more than 30% for rapid growth), with tabs separating the data.

  3. The results of the program are shown below.

Mobile phone companyIs there rapid growth?
SamsungNo
AppleNo
HuaweiNo
OPPOFast
VivoFast
ZTENo
LGNo
LenovoNo
Xiaomino
import os

def isBigGrowth(L,rate):
if float (L[2])>float(L[1])*(1+rate) and float(L[3])>float(L[2])*(1+rate) and float(L[4])>float(L[3])*(1+rate):
return "快速"
else:
return "否"

with open(r"文件路径\smartPhone.txt") as f:
line = f.read().strip()
linestr = line.split("\n") # 以换行符分隔
del linestr[0]
print("手机公司 是否快速增长?")
for s in linestr:
try:
L = s.split('\t')
print(L[0], end=" ")
print(isBigGrowth(L, 0.3))
except:
print('运行失败')

Results of the run. 在这里插入图片描述