In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to use Python to read CSV files and calculate the mean and variance of a column, many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
Recently, it is necessary to deal with the csv file of excel to obtain a series of data such as the mean and variance of a bank's stock price over the years.
The composition of the file is very simple, some of which are as follows
There are nearly 7, 000 rows of data, the main work is to extract the stock price data, put it into an array, and then use the numpy module to find out the required data.
Here, the csv module is used to process the files, and the final implementation code is as follows:
Import csvimport numpy as npwith open ('pingan_stock.csv') as csv_file: row = csv.reader (csv_file, delimiter=',') next (row) # read the first line price = [] # create an array to store stock price data # read the second column of each row except the first row And add it to the array price for rin row: price.append (float (r [1])) # convert string data to floating point and add it to the array print (np.var (price)) # output mean print (np.mean (price)) # output variance
First of all, use the reader method of csv, where delimiter is optional, it is a delimiter, and the original value is a comma, so it doesn't matter whether you add it or not.
Reader returns an object that can be iterated, which needs to be traversed using a for loop. Some of the output values of row are as follows:
The function of next (row) is to read the first list, that is, ['year','price']. Because the character data is converted into floating point data in the later code to facilitate the final calculation, this code must be added, otherwise an error will be reported during the conversion. When traversing the row, r [1] represents the second data of each row, and uses append to store the second column data of each row into the array. When the traversal is completed, all the data in the second column is successfully stored in the price array, and all the floating-point data are obtained. Even if the array is finished, the mean and variance are calculated by using the mean and var of the numpy module.
After reading the above, do you have any further understanding of how to use Python to read CSV files and calculate the mean and variance of a column? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.