A note on how to read the file when using the table data of the csv file saved locally in python.
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.
import pandas as pd
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".
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.
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 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
For more information, click here [https://qiita.com/yuta-38/items/e1e890a647e77c7ccaad)
Recommended Posts