Financial data analysis (1) Python warm-up: dictionary sorting, calculating annual growth rate

Welcome to study financial data analysis together

I recently entered the factory for an internship in a position related to Internet finance, and I found that what I learned is a little bit charming. So I decided to take the time to list a few related cases and learn from time to time.

Case (1) Python warm-up

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

Project 1: Dictionary Statistics Sorting

The dictionary d stores the correspondence between my country's 42 double first-class universities and their provinces. 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 province with the largest number and the number. ‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬

d = {"Peking University": "Beijing", "Renmin University of China": "Beijing", "Tsinghua University": "Beijing",
"Beijing University of Aeronautics and Astronautics": "Beijing", "Beijing Institute of Technology": "Beijing" ", "China Agricultural University": "Beijing",
"Beijing Normal University": "Beijing", "Central University for Nationalities": "Beijing", "Nankai University": "Tianjin",
"Tianjin University": "Tianjin", "Dalian University of Technology": "Liaoning", "Jilin University": "Jilin",
"Harbin Institute of Technology": "Heilongjiang", "Fudan University": "Shanghai", "Tongji University": "Shanghai",
"Shanghai Jiaotong University": "Shanghai", "East China Normal University": "Shanghai", "Nanjing University": "Jiangsu",
"Southeast University": "Jiangsu", "Zhejiang University": "Zhejiang", "University of Science and Technology of China" : "Anhui",
"Xiamen University": "Fujian", "Shandong University": "Shandong", "Ocean University of China": "Shandong",
"Wuhan University": "Hubei", "Huazhong University of Science and Technology": "Hubei" ", "Central South University": "Hunan",
"Sun Yat-sen University": "Guangdong", "South China University of Technology": "Guangdong", "Sichuan University": "Sichuan",
"University of Electronic Science and Technology": "Sichuan"," "Chongqing University": "Chongqing", "Xi'an Jiaotong University": "Shaanxi",
"Northwestern Polytechnical University": "Shaanxi", "Lanzhou University": "Gansu", "National University of Defense Technology": "Hunan",
"Northeastern University" ": "Liaoning", "Zhengzhou University": "Henan", "Hunan University": "Hunan", "Yunnan University": "Yunnan",
"Northwest A&F University": "Shaanxi", "Xinjiang University":" Xinjiang"}

Output format
Province: Quantity (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]))

operation result:
pg1

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

1. The file smartphone.txt stores the annual sales data of some companies' mobile phones, each row has a number of years of sales (in millions) for each company, and the separator between the data is a tab.

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

The content of the smartPhone.txt file is as follows:

公司	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

3. Write the function isBigGrowth(L,rate), the formal parameter L is a set of lists containing numerical data (a company's sales in each year), and rate is the annual growth rate. Determine and return whether the annual sales volume is growing rapidly: if the annual sales volume If the growth rate exceeds the given rate, it is True, otherwise it is False.

4. The main program reads the data in smartphone.txt, converts the annual sales in each row of data into numerical data, and uses the function isBigGrowth(L,rate) to calculate and screen output whether the company’s annual sales are growing rapidly (this question) It is assumed that the annual sales growth rate exceeds 30% as rapid growth), and the data is separated by tabs.

5. The running result of the program is shown in the figure below:

Cell phone company Is it growing fast?
Samsung no
Apple no
Huawei no
OPPO fast
Vivo fast
ZTE no
LG no
Lenovo no
Xiaomi no
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('运行失败')

operation result:
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_43082153/article/details/108576220