This article is the third day of Furukawa Lab Advent_calendar. This article was written by a student at Furukawa Lab as part of his studies. The content may be ambiguous or the expression may be slightly different.
In this article, I'll summarize the commands that were useful when I used pandas for data formatting. It will be sent by beginners of the program, so it would be helpful if you could see it with warm eyes ^^
python
import pandas as pd
df=pd.read_csv('File Path')
Basically read csv with this Actually, there may be situations where you have to read several files, so the method used in such cases is shown below.
python
import glob
#Same hierarchy
file_pass = glob.glob('*.csv')
#You can also specify the hierarchy
file_pass = glob.glob('○○/○○/*.csv')
This will take the path of the .csv file in the specified hierarchy. In the directory ○○ / ○○ data_1.csv , data_1.txt , data_2.csv , data_2.txt If exists
python
[○○/○○/data_1.csv,○○/○○/data_2.csv]
Is returned. The rest is a for statement
python
counter = -1
for i in file_pass
df = pd.read_csv(i)
counter = counter + 1
#Add some operation
#If you want to save again and save without index, index=Just add False
df.to_csv('new_name_{0}.csv'.format(counter))
You can format the data at once (to_csv, counter, etc.)
os This is convenient when naming
python
import os
# ()Bring the path inside"../"And refer to the one above the place where this code is written
path = os.path.abspath(filepath)
#Bring the file name out of the path
#It was convenient to use with glob
name = os.path.basename(filepath)
#Sometimes I don't need an extension.Split and split with
name = name.split(".")
name = name[0]
It was said that it is easy to read the csv file at once and add the same operation when using glob and os. I thought when I was doing it myself, but the operation of pandas itself comes out if I google it like "pandas ○○", but I can not judge whether it is a pandas function or a python library and it works well There were many situations where I couldn't search. I want to know what I can do and develop the ability to google properly ╭ (・ ㅂ ・) و
Recommended Posts