It's almost time to use Pandas for business, so I don't know what number to brew, but I would like to summarize the frequently used processes.
import pandas as pd
#File name: sample.csv
#The contents of the file are described below.
# 1,Sample Taro,30
# 2,Sample Hanako,25
# 3,Sample Jiro,28
df = pd.read_csv('sample.csv',
#If None, there is no header, and if you specify a number, that line is interpreted as a header.
header=None,
#Specify the column to be indexed * Here, the first column(1,2,Part 3)
index_col=0,
#Define column name
names=('name', 'age'),
#Define the data type of the column
dtype={'name': str, 'age': int}
)
import pandas as pd
#File name: sample.xlsx
#* The contents of the file are the same as the above CSV file.
df = pd.read_excel('sample.xlsx',
dtype={'name': str, 'age': int}
)
import pandas as pd
excel_book = pd.ExcelFile('sample.xlsx')
#You can get a list of sheet names of the loaded sheets in a list.
sheet_name_list = excel_book.sheet_names
#If you specify a sheet name, you can get the contents of that sheet.
df = excel_book.parse('sheet1')
import pandas as pd
#The first argument is the output file name
df.to_csv('sample.csv',
#Set False if header is not needed
header=False,
#Set False if no index is needed
index=False,
#Set when limiting the output columns
columns=['name']
)
import pandas as pd
#The first argument is the output file name
df.to_excel('sample.xlsx',
#Set False if header is not needed
header=False,
#Set False if no index is needed
index=False,
#Set when limiting the output columns
columns=['name']
)
Recommended Posts