[Python] How to read a csv file (read_csv method of pandas module)

[Note] How to read a csv file with python (read_csv method of pandas module)

A note on how to read the file when using the table data of the csv file saved locally in python.

How to do

Use the read_csv method of the pandas module.

** ■ Read_csv syntax ** pandas.read_csv ('csv file path') └ The path can be either an absolute path or a relative path. └ Pandas is often abbreviated as pd and imported.


** ▼ How to import pandas as pd **

import pandas as pd


## Actual usage Basically, the acquired data is stored in ** variables **.

As a variable, ** df ** (abbreviation of dataframe) is often used.

If you do not store it in a variable, the contents will be output when it is read.

** ▼ What is DataFrame ** ・ A type of data format ・ Iwaruyu table data (excel image) -By setting the variable to df, it is easy to understand that "this is table data".

Illustration

① Read with absolute path

Load test.csv on the desktop. df = pd.read_csv('~/desktop/test.csv') └ "df": Arbitrary variable  └「pd」:pandas └ ".read_csv ('')": Method to read csv with pandas └ "~ / desktop / test.csv": Specify the test.csv file of the desktop with the full path.


▼ Read the csv file with an absolute path, Check the contents with the print function.

python


import pandas as pd

df = pd.read_csv('~/desktop/test.csv')
print(df)


#Output example

#         date      start       high        low        end   adjusted
#0    2020/3/9  20,343.31  20,347.19  19,472.26  19,698.76  19,698.76
#1    2020/3/6  21,009.80  21,061.20  20,613.91  20,749.75  20,749.75
#2    2020/3/5  21,399.87  21,399.87  21,220.76  21,329.12  21,329.12
#3    2020/3/4  20,897.20  21,245.93  20,862.05  21,100.06  21,100.06

### ② Read by relative path

Read test-same-directory.csv in the same hierarchy as the .py file. df = pd.read_csv('test-same-directory.csv') └ "df": Arbitrary variable  └「pd」:pandas └ ".read_csv ('')": Method to read csv with pandas └ "test-same-directory.csv": Read the specified file in the same directory.

python


import pandas as pd

df2 = pd.read_csv(''test-same-directory.csv'')
print(df2)


#Output example

#         date      start       high        low        end   adjusted
#0    2020/3/9  20,343.31  20,347.19  19,472.26  19,698.76  19,698.76
#1    2020/3/6  21,009.80  21,061.20  20,613.91  20,749.75  20,749.75
#2    2020/3/5  21,399.87  21,399.87  21,220.76  21,329.12  21,329.12
#3    2020/3/4  20,897.20  21,245.93  20,862.05  21,100.06  21,100.06

Various options can be set in read_csv. Specify the rows and columns to read, read and open files on the WEB, etc.

For more information, click here [https://qiita.com/yuta-38/items/e1e890a647e77c7ccaad)

Recommended Posts

[Python] How to read a csv file (read_csv method of pandas module)
How to read a CSV file with Python 2/3
[Python] How to read excel file with pandas
How to convert JSON file to CSV file with Python Pandas
[Python] How to output a pandas table to an excel file
How to read an Excel file (.xlsx) with Pandas [Python]
[Python] How to convert db file to csv
[Python] Summary of how to use pandas
How to read CSV files in Pandas
[Python] How to force a method of a subclass to do something specific
[Python] How to store a csv file as one-dimensional array data
How to create a JSON file in Python
Read CSV file: pandas
How to add a Python module search path
How to put a line number at the beginning of a CSV file
Read Python csv file
How to read a file in a different directory
Summary of how to read numerical data with python [CSV, NetCDF, Fortran binary]
[Introduction to Pandas] Read a csv file without a column name and give it a column name
Template of python script to read the contents of the file
How to write a list / dictionary type of Python3
How to output CSV of multi-line header with pandas
[Python] How to deal with pandas read_html read error
[Python] A memo to write CSV vertically with Pandas
How to save a table scraped by python to csv
Python script to create a JSON file from a CSV file
How to read csv containing only integers in Python
Read csv with python pandas
Every time I try to read a csv file using pandas, I get a numpy error.
When reading a csv file with read_csv of pandas, the first column becomes index
[Python] How to make a list of character strings character by character
How to switch the configuration file to be read by Python
How to run a Python file at a Windows 10 command prompt
How to shuffle a part of a Python list (at random.shuffle)
How to use the __call__ method in a Python class
How to import a file anywhere you like in Python
I tried "How to get a method decorated in Python"
How to develop in a virtual environment of Python [Memo]
How to get a list of built-in exceptions in python
Python> Read from a multi-line string instead of a file> io.StringIO ()
How to override a user-defined method generated by python swig
How to write a Python class
[Python] Write to csv file with Python
[Python] How to use Pandas Series
Output to csv file with Python
Etosetra related to read_csv of Pandas
[Python] How to delete rows and columns in a table (list of drop method options)
How to create a config file
Points to note when making pandas read csv of excel output
Python --Most elegant way to read lines of file into list
How to determine the existence of a selenium element in Python
How to check the memory size of a variable in Python
[Python] How to add rows and columns to a table (pandas DataFrame)
How to make a command to read the configuration file with pyramid
How to find the memory address of a Pandas dataframe value
How to output the output result of the Linux man command to a file
How to create a CSV dummy file containing Japanese using Faker
[Python] Read a csv file with a large data size using a generator
[ROS2] How to play a bag file with python format launch
DataFrame of pandas From creating a DataFrame from two lists to writing a file
[Python] How to make a matrix of repeating patterns (repmat / tile)