[PYTHON] Etosetra related to read_csv of Pandas

The read_csv function of the Pandas module is often used when reading CSV files in Python programs. The writing style that you often see in sample programs is like this.

import pandas as pd
df = pd.read_csv('./iris.csv')

It is Etosetra related to such read_csv.

Read files directly on the internet

Not only files on the PC but also files on the Internet can be read directly by specifying the URL. An example is the Pandas iris dataset on Github.

url = 'https://github.com/pandas-dev/pandas/raw/master/pandas/tests/data/iris.csv'
df = pd.read_csv(url)

Reads the specified number of lines

This is convenient when you want to read a long file.

df = pd.read_csv(url, nrows=10)

Specify the column and type to read

Only specific columns can be read.

df = pd.read_csv(url, usecols=['SepalLength', 'SepalWidth'])

It is also possible to read by specifying the type.

df = pd.read_csv(url, usecols=['SepalLength', 'SepalWidth'], dtype={'SepalLength': float, 'SepalWidth': float})
#Type confirmation
df.dtypes

Read an Excel file

It can also be read from Excel. Introducing read_excel, a friend of read_csv. The xlrd module is required, so let's install it.

pip install xlrd

The usage is the same as read_csv. As expected it is a friend.

dfx = pd.read_excel('iris.xlsx')

Write to CSV file

It is humanity that you want to write after reading.

Copy to clipboard

There is also such an instruction. You can save the trouble of selecting and copying.

dfx.to_clipboard()

Write to CSV file

Use to_csv. It will be saved in the specified file path.

dfx.to_csv('iris_out.csv')

If you enclose it in print, the result of csv will be displayed on the screen.

print(dfx.to_csv())

Writing to an excel file

If you read it from Excel, you will want to write it. I will use to_excel. Install the openpyxl module as it is required.

!pip install openpyxl

Usage is the same as to_csv.

dfx.to_excel('iris_out.xlsx')

I was surprised to be able to read the latest Office 365 Excel file. As expected.

Recommended Posts

Etosetra related to read_csv of Pandas
[Python] Summary of how to use pandas
pandas related links
[Python] How to read a csv file (read_csv method of pandas module)
Origin of that name related to programming # 1 Language name 1
Header shifts in read_csv () and read_table () of Pandas
Arrangement of self-mentioned things related to machine learning
How to use Pandas 2
Convert 202003 to 2020-03 with pandas
Basic operation of pandas
About MultiIndex of pandas
UnicodeDecodeError in pandas read_csv
Basic operation of Pandas
How to output CSV of multi-line header with pandas
How to get an overview of your data in Pandas
[Introduction to cx_Oracle] Overview of cx_Oracle
Allocation of resources to testing
Formatted display of pandas DataFrame
Key additions to pandas 1.1.0 and 1.0.0
How to use Pandas Rolling
Basic usage of Pandas Summary
Behavior of pandas rolling () method
Index of certain pandas usage
Export pandas dataframe to excel
The Power of Pandas: Python
Points to note when making pandas read csv of excel output
I tried to get a database of horse racing using Pandas
How to find the memory address of a Pandas dataframe value
What to do if "Unnamed: 0" is added in to_csv-> read_csv in pandas
What to do when UnicodeDecodeError occurs during read_csv in pandas (pd.read_table ())
DataFrame of pandas From creating a DataFrame from two lists to writing a file