We have summarized how to read csv files (txt etc. are also possible) in the folder in order. I wrote it for those who want to know how to automatically read in order instead of writing the instructions to read files one by one when reading all the large number of csv files in an arbitrary folder. It is assumed that you know how to read individual csv files.
First, import it.
import os
import pandas as pd
Next, copy the address of the folder where the csv files you want to read are stored. From here, let's take a quick look.
#Paste the copied address.
csv_file=os.listdir('C:/Users/Satoru Mizu/Documents/Qiita')
print(csv_file)
>['First day.csv', 'the 2nd day.csv']
print(csv_file[0])
>First day.csv
#First, let's read the first file
file1=pd.read_csv('C:/Users/Satoru Mizu/Documents/Qiita/'+str(csv_file[0]),engine='python')
With the above operation, the first file among a large number of csv files can be read. When I check the contents,
print(file1)
>Column 1 Column 2 Column 3 Column 4
>0 A-kun B-kun C-kun D-kun
>1 1 2 3 4
>2 9 10 11 12
I was able to read it properly.
By using for minutes, you can write code that reads and processes csv files in order. A simple example is shown below.
Calculate the total score of Mr. A
A_sum=0
for i in range(len(csv_file)): #Turn the for statement as many times as there are csv files.
file =pd.read_csv('C:/Users/Satoru Mizu/Documents/Qiita/'+str(csv_file[i]),engine='python')
A_sum += file.iloc[1][0] + file.iloc[2][0]
```
# 2. Pattern to paste the path of all files into csv
First, select all the csv files in the folder where the csv files are stored.
In that state, hold down "Shift" and right-click.
I think a "copy of the path" will appear, so copy it.
After that, create a new csv file (txt is also acceptable) in the folder where the program is running and paste it.
In my case, it looks like this.
C: \ Users \ Mizugoro \ Documents \ Qiita \ Day 1 .csv
C: \ Users \ Mizugoro \ Documents \ Qiita \ Day 2 .csv
Here, replace "\" with "/" (Ctrl + F). Then it looks like this
C: / Users / Satoru Mizu / Documents / Qiita / Day 1 .csv
C: / Users / Satoru Mizu / Documents / Qiita / Day 2.csv
```
csv_adress=pd.read_csv('File with pasted path.csv',engine='python')
print(csv_adress.iloc[0])
>adress C:/Users/Satoru Mizu/Documents/Qiita/First day.csv
>Name: 0, dtype: object
```
Therefore, it cannot be used as a path when opening a csv file.
However,
```
print(*csv_adress.iloc[0])
> C:/Users/Satoru Mizu/Documents/Qiita/First day.csv
```
Then, it will be only the path.
```
file2=pd.read_csv(*csv_adress.iloc[0],engine='python')
print(file2)
>Column 1 Column 2 Column 3 Column 4
>0 A-kun B-kun C-kun D-kun
>1 1 2 3 4
>2 9 10 11 12
```
I opened it properly ~
Thank you for your hard work! !!
Recommended Posts