Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the process of Python data analysis?

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the relevant knowledge of "what is the process of Python data analysis". The editor shows you the operation process through an actual case. The operation method is simple and fast, and it is practical. I hope this article "how the process of Python data analysis is" can help you solve the problem.

I. introduction of requirements

The main requirement is to analyze the historical data of a certain kind of data.

The customer's demand is based on the situation of the first two periods of the data, if there is a certain oblique two numbers equal, then buy the lottery ticket in the same location of the third period.

For 1, if the equivalent number is 1-5, buy 6-10, if the equivalent number is 6-10, buy 1-5

For 2, if the equivalent number is 1-5, buy 1-5, and if the equivalent number is 6-10, buy 6-10.

Then, according to this scheme, it is possible to buy it, but it is also possible to miss it, so the customer hopes that I can count it out in 100 days, according to this method, six or more consecutive purchases of lottery tickets can win a prize and the corresponding time, for this case, we will analyze in detail below.

Second, take the first and the first as an example for data analysis

Here, we first use Jupyter Notebook to analyze, and then, after getting the results, we use Pycharm to complete the program design.)

1. Get a day's data

Open the interface shown in the following figure to get the URL and the request header:

1. Website (website of historical data)

2. Request header

Then we write the code in the program to get the data:

Then do some pre-processing:

2. Start the analysis of the data of the day

Here we show the code directly:

Def reverse_list (lst): "" order of quasi-swap list: param lst: original list: return: new list "return [ele for ele in reversed (lst)] low_list = [" 01 "," 02 "," 03 "," 04 "," 05 "] # set the list of smaller numbers high_list = [" 06 "," 07 "," 08 "," 09 " "10"] # set a list of relatively large numbers N = set a number N to record how many periods can be purchased n = set a number n to record how many lottery tickets record_number = 1 # set a judgment value of record data list_data_number = [] # set an empty list to store the number of consecutive failed periods in a day dict _ time_record = {} # set an empty dictionary to store the time node for k in range (1152): # loop through all data points if k for the number of consecutive periods that meet the listed conditions

< 1150: new_result1 = reverse_list(new_response["result"]["data"])[k] # 第一期数据 new_result2 = reverse_list(new_response["result"]["data"])[k + 1] # 第二期数据 new_result3 = reverse_list(new_response["result"]["data"])[k + 2] # 第三期数据 data1 = new_result1['preDrawCode'].split(',') # 第一期数据 data2 = new_result2['preDrawCode'].split(',') # 第二期数据 data3 = new_result3['preDrawCode'].split(',') # 第三期数据 for m in range(10): # 通过循环来判断是否满足购买的条件,并且实现一定的功能 if m == 0: if data2[0] == data1[1]: # 如果相等就要结束循环 N += 1 # 可以购买的期数应该要自加一 if (data2[0] in low_list and data3[0] in low_list) or (data2[0] in high_list and data3[0] in high_list): n += 1 # 命中的期数应该要自加一 # 如果命中了的话,本轮结束,开启下一轮 list_data_number.append(record_number) if f"{record_number}" in dict_time_record.keys(): # 如果已经有了这个键,那么值添加时间点 dict_time_record[f"{record_number}"].append(new_result3['preDrawTime'][11:]) else: # 如果没有这个键,那么添加一个键值对,值为一个列表,而且初始化为当前的时间 dict_time_record[f"{record_number}"] = [new_result3['preDrawTime'][11:]] record_number = 1 # 初始化下一轮的开始 else: record_number += 1 # 如果没有命中的话,次数就应该要自加一 break # 如果满足相等的条件就要结束循环 elif m == 9: # 与上面差不多的算法 if data2[9] == data1[8]: # 如果相等 N += 1 if (data2[9] in low_list and data3[9] in low_list) or (data2[9] in high_list and data3[9] in high_list): n += 1 list_data_number.append(record_number) if f"{record_number}" in dict_time_record.keys(): dict_time_record[f"{record_number}"].append(new_result3['preDrawTime'][11:]) else: dict_time_record[f"{record_number}"] = [new_result3['preDrawTime'][11:]] record_number = 1 else: record_number += 1 break else: # 与上面差不多的算法 if data2[m] == data1[m + 1] or data2[m] == data1[m - 1]: # 如果相等 N += 1 if (data2[m] in low_list and data3[m] in low_list) or (data2[m] in high_list and data3[m] in high_list): n += 1 list_data_number.append(record_number) if f"{record_number}" in dict_time_record.keys(): dict_time_record[f"{record_number}"].append(new_result3['preDrawTime'][11:]) else: dict_time_record[f"{record_number}"] = [new_result3['preDrawTime'][11:]] record_number = 1 else: record_number += 1 breakprint(f"日期:{new_response['result']['data'][0]['preDrawTime'][:10]},总的梯子数为{N}个,一共有{n}次命中,一共有{N - n}次挂了")# 打印时间,以及,可以购买的期数,命中的期数,没有命中的期数list_data_number.sort()# 按照大小顺序来进行排序dict_record = {}# 设置空字典进行记录for i in list_data_number: if f"{i}" in dict_record.keys(): # 判断是否已经有了这个数字? dict_record[f"{i}"] += 1 # 如果有的话,那么就会自加一 else: # 如果没有的话,那么就会创建并且赋值等于 1 dict_record[f"{i}"] = 1 # 创建一个新的字典元素,然后进行赋值为 1for j in dict_record.keys(): if (int(j) >

= 6) and (int (j)

< 15): # 实际的结果表明,我们需要的是大于等于6期的数据,而没有出现大于15的数据,因此有这样的一个关系式 print(f"买{j}次才中奖的次数为{dict_record[j]}") # 打印相关信息 print(dict_time_record[j]) str0 = "" for letter in dict_time_record[j]: str0 += letter str0 += ", " print(str0) # 打印相关信息 运行结果的展示如下图所示: 3、循环日期进行多天的数据分析: 首先设置一个事件列表来记录需要统计哪些天的数据: 代码: data_list = []for h in range(31): data_list.append(f'1-{h + 1}')for h in range(28): data_list.append(f'2-{h + 1}')for h in range(31): data_list.append(f'3-{h + 1}')for h in range(20): data_list.append(f'4-{h + 1}') 通过上述的代码,我们即实现了时间列表的设置,然后我们循环遍历这个列表访问不同日期的彩票数据即就是得到了不同时间的数据,然后再利用上述的分析方法来进行数据分析,即就是可以得到了多天的彩票数据分析的结果了。 4、将数据写入Excel表格中 这里我们可以采用xlwt 模块来进行excel表格的写入操作啦,具体的写入就不必过多赘述了。 三、完整的代码展示: 一下是完整的代码: import requestsimport chardetimport jsonimport xlwt # excel 表格数据处理的对应模块def reverse_list(lst): """ 准换列表的先后顺序 :param lst: 原始列表 :return: 新的列表 """ return [ele for ele in reversed(lst)]data_list = []for h in range(31): data_list.append(f'1-{h + 1}')for h in range(28): data_list.append(f'2-{h + 1}')for h in range(31): data_list.append(f'3-{h + 1}')for h in range(20): data_list.append(f'4-{h + 1}')wb = xlwt.Workbook() # 创建 excel 表格sh = wb.add_sheet('彩票分析数据处理') # 创建一个 表单sh.write(0, 0, "日期")sh.write(0, 1, "梯子数目")sh.write(0, 2, "命中数目")sh.write(0, 3, "挂的数目")sh.write(0, 4, "6次中的数目")sh.write(0, 5, "6次中的时间")sh.write(0, 6, "7次中的数目")sh.write(0, 7, "7次中的时间")sh.write(0, 8, "8次中的数目")sh.write(0, 9, "8次中的时间")sh.write(0, 10, "9次中的数目")sh.write(0, 11, "9次中的时间")sh.write(0, 12, "10次中的数目")sh.write(0, 13, "10次中的时间")sh.write(0, 14, "11次中的数目")sh.write(0, 15, "11次中的时间")sh.write(0, 16, "12次中的数目")sh.write(0, 17, "12次中的时间")sh.write(0, 18, "13次中的数目")sh.write(0, 19, "13次中的时间")sh.write(0, 20, "14次中的数目")sh.write(0, 21, "14次中的时间")# wb.save('test4.xls')sheet_seek_position = 1# 设置表格的初始位置为 1for data in data_list: low_list = ["01", "02", "03", "04", "05"] high_list = ["06", "07", "08", "09", "10"] N = 0 n = 0 url = f'https://api.api68.com/pks/getPksHistoryList.do?date=2021-{data}&lotCode=10037' headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) ' 'AppleWebKit/537.36 (KHTML, like Gecko) ' 'Chrome/90.0.4430.72 Safari/537.36' } response = requests.get(url=url, headers=headers) response.encoding = chardet.detect(response.content)['encoding'] new_response = json.loads(response.text) sh.write(sheet_seek_position, 0, new_response['result']['data'][0]['preDrawTime'][:10]) # 在表格的第一个位置处写入时间,意即:data record_number = 1 # 记录数据的一个判断值,设置为第一次,应该是要放在最外面的啦 list_data_number = [] # 设置一个空列表来存储一天之中的连续挂的期数 dict_time_record = {} for k in range(1152): # record_number = 1,应该要放外面 # 记录数据的一个判断值,设置为第一次 if k < 1150: new_result1 = reverse_list(new_response["result"]["data"])[k] new_result2 = reverse_list(new_response["result"]["data"])[k + 1] new_result3 = reverse_list(new_response["result"]["data"])[k + 2] data1 = new_result1['preDrawCode'].split(',') data2 = new_result2['preDrawCode'].split(',') data3 = new_result3['preDrawCode'].split(',') for m in range(10): if m == 0: if data2[0] == data1[1]: N += 1 if (data2[0] in low_list and data3[0] in high_list) or (data2[0] in high_list and data3[0] in low_list): n += 1 # 如果命中了的话,本轮结束,开启下一轮 list_data_number.append(record_number) if f"{record_number}" in dict_time_record.keys(): dict_time_record[f"{record_number}"].append(new_result3['preDrawTime'][11:]) else: dict_time_record[f"{record_number}"] = [new_result3['preDrawTime'][11:]] # print(record_number) record_number = 1 # 初始化 else: record_number += 1 # 没中,次数加一 # 自加一 break elif m == 9: if data2[9] == data1[8]: N += 1 if (data2[9] in low_list and data3[9] in high_list) or (data2[9] in high_list and data3[9] in low_list): n += 1 list_data_number.append(record_number) if f"{record_number}" in dict_time_record.keys(): dict_time_record[f"{record_number}"].append(new_result3['preDrawTime'][11:]) else: dict_time_record[f"{record_number}"] = [new_result3['preDrawTime'][11:]] # print(record_number) record_number = 1 else: record_number += 1 break else: if data2[m] == data1[m + 1] or data2[m] == data1[m - 1]: N += 1 if (data2[m] in low_list and data3[m] in high_list) or (data2[m] in high_list and data3[m] in low_list): n += 1 list_data_number.append(record_number) if f"{record_number}" in dict_time_record.keys(): dict_time_record[f"{record_number}"].append(new_result3['preDrawTime'][11:]) else: dict_time_record[f"{record_number}"] = [new_result3['preDrawTime'][11:]] # print(record_number) record_number = 1 else: record_number += 1 break print(f"日期:{new_response['result']['data'][0]['preDrawTime'][:10]},总的梯子数为{N}个,一共有{n}次命中,一共有{N - n}次挂了") sh.write(sheet_seek_position, 1, N) sh.write(sheet_seek_position, 2, n) sh.write(sheet_seek_position, 3, N - n) # new_list_data_number = list_data_number.sort() list_data_number.sort() # 进行排序 dict_record = {} # 设置空字典 for i in list_data_number: if f"{i}" in dict_record.keys(): # 判断是否已经有了这个数字? dict_record[f"{i}"] += 1 # 如果有的话,那么就会自加一 else: # 如果没有的话,那么就会创建并且赋值等于 1 dict_record[f"{i}"] = 1 # 创建一个新的字典元素,然后进行赋值为 1 # print(dict_record) # print(f"买彩票第几次才中奖?") # print(f"按照我们的规律买彩票的情况:") for j in dict_record.keys(): if (int(j) >

= 6) and (int (j) < 15): print (f "buy {j} times before winning the prize is {dict_ record [j]}") print (dict_time_ record [j]) str0 = "" for letter in dict_time_record [j]: str0 + = letter str0 + = " "print (str0) sh.write (sheet_seek_position, 4 + (int (j)-6) * 2, dict_ record [j]) # write sh.write (sheet_seek_position, 4 + (int (j)-6) * 2 + 1 Str0 [:-2]) # Note that it should be changed to-2 # the corresponding time for writing several times # print (j) sheet_seek_position + = 1 # after each time To wrap the position to the next line, so as to facilitate the writing of the next line # save wb.save ('extreme airship lottery analysis results.xls')

Show the running results:

Show 1.

Show 2.

Thus, we have solved the data analysis of the lottery ticket of the high-speed airship.

Then, we only need to change the algorithm a little bit, and the rest of the algorithm is exactly the same, so that we can analyze the data of high-speed cars.

The modified code is listed below:

For m in range (10): if m = = 0: if data2 [0] = = data1 [1]: n + = 1 if (data2 [0] in low_list and data3 [0] in low_list) or (data2 [0] in high_list and data3 [0] in high_list): N + = 1 # if it is hit The end of this round Open the next round of list_data_number.append (record_number) if f "{record_number}" in dict_time_record.keys (): dict_time_record [f "{record_number}"] .append (new_result3 ['preDrawTime'] [11:]) Else: dict_time_record [f "{record_number}"] = [new_result3 ['preDrawTime'] [11:]] # print (record_number) record_number = 1 # initialize else: Record_number + = 1 # missed Times plus one # self plus one break elif m = 9: if data2 [9] = = data1 [8]: n + = 1 if (data2 [9] in low_list and data3 [9] in low_list) or (data2 [9) ] in high_list and data3 [9] in high_list): n + = 1 list_data_number.append (record_number) if f "{record_number}" in dict_time_record.keys (): dict_time_record [f "{record_number } "] .append (new_result3 ['preDrawTime'] [11:]) else: dict_time_record [f" {record_number} "] = [new_result3 [' preDrawTime'] [11:]] # print (record_number) record_number = 1 else: record_number + = 1 break else: if data2 [m] = = data1 [m + 1] or data2 [m] = data1 [m-1]: n + = 1 if (data2 [m) ] in low_list and data3 [m] in low_list) or (data2 [m] in high_list and data3 [m] in high_list): n + = 1 list_data_number.append (record_number) if f "{record_number}" in dict_time_record.keys (): Dict_time_record [f "{record_number}"] .append (new_result3 ['preDrawTime'] [11:]) else: dict_time_record [f "{record_number}"] = [new_result3 [' preDrawTime'] [11:]] # print (record_ Number) record_number = 1 else: record_number + = 1 break on "what is the process of Python data analysis"? Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report