Numpy and pandas library combat-batch get the first column of data in multiple CSV files in the folder and find the best value (with a stage video tutorial)

A few days ago, a small partner asked a question about Python reading the first column of data in multiple CSV files in the folder and finding the maximum and minimum values. The discussion is still quite intense. Here are two summary This is a method, and I hope that some friends who encounter this problem can avoid detours. (Video tutorials for each stage of Python are attached at the end of the article, you can find me a free prostitute)


/1. Problem description/

If you want to find the maximum or minimum value in CSV or Excel, we can find it with the help of the built-in functions max() and min() in Excel. If it's just one or two files, it's definitely a piece of cake to process. When the number of files reaches hundreds, if you follow this method again, it will take time and effort, and you will get half the effort.

But white panic, for the multiple CSV files in the figure below, we can use Python to traverse and read multiple files at once, and then process the files separately, which is twice the result with half the effort.

Numpy and pandas combat: the first column of data in the folder CSV file

 

/Two, the solution/

1. First, let's take a look at the content of the file, here is the content of one of the files, as shown in the figure below.

Numpy and pandas combat: the first column of data in the folder CSV file

 

Of course, this is only a small part of the content of the file, and the actual amount of data is definitely not 21.

2. Now we want to operate on the data in the first column or the second column. Take the maximum and minimum values ​​as an example. Here, the first column is the target data for evaluation.

Numpy and pandas combat: the first column of data in the folder CSV file

 

Usually we use Python to process data. The two most used libraries are numpy and pandas. In this article, we will use the two libraries to operate.

3. The pandas library is used to read the first column of data in multiple CSV files in the folder and find the maximum and minimum values ​​as shown in the figure below.

Numpy and pandas combat: the first column of data in the folder CSV file

 

4. The result obtained through the pandas library is shown in the figure below.

Numpy and pandas combat: the first column of data in the folder CSV file

 

With this method, you can quickly get the maximum and minimum values ​​in the first column of all files in the folder.

5. The code below uses the numpy library to read the first column of data in multiple CSV files in the folder and find the maximum and minimum values ​​as shown in the figure below.

Numpy and pandas combat: the first column of data in the folder CSV file

 

6. The result obtained through the numpy library is shown in the figure below.

Numpy and pandas combat: the first column of data in the folder CSV file

 

Through this method, you can also quickly get the maximum and minimum values ​​in the first column of all files in the folder.

 

/summary/

This article is based on Python, using the numpy library and pandas library to read multiple CSV files in the folder, and find the maximum and minimum values ​​of the first column of data in the file. Of course, in addition to these two methods, there must be Other methods can also be done, and you are welcome to actively discuss it.

 

If you want to learn more about Python, it's very easy. This compiled video tutorial is waiting for you to learn:

 

1. 2020 version of Python tutorial: from entry to Python engineer level https://pan.baidu.com/s/1qNo-YoUK90XIiDV7mTJk8w Extraction code: rf 

2. 2020 _Python_ Five Days Fun Linux Tutorial https://pan.baidu.com/s/1eMlTxUqC8VEkNMwEjE0nsw Extraction code: y

3. 2020 latest_Python_(MySQL_SQL_Redis) database detailed explanation https://pan.baidu.com/s/1uz4hBij5xSjgd5Tom72NzQ extraction

4. Django easily realizes blog website https://pan.baidu.com/s/1BkdIJ-9b85R9pSnMBGFRCw Extraction code: s7

 

For those who are interested in learning Python, or want to improve their Python programming skills, you can find me for a tutorial (reply: Python) ~~~ Selfless for you~ 

If you are private, the blog system can only reply 3 times a day, so I will return to you, you must return to me, otherwise I cannot reply to the fourth partner!

Guess you like

Origin blog.csdn.net/GUDUzhongliang/article/details/108726410