Python pandas is a good library to read csv file. We will introduce how to read csv data in this tutorial for python beginners. You can learn how to do by following our tutorial.
You can use pip to install pandas library first.
pip install pandas
Analysis csv file
You should notice the header and separation character of a csv file. Here is an example.
Read data from a csv file using python pandas
Create a csv file and write some data. You can use code below to read csv file using pandas.
import pandas as pd file = r'data/601988.csv' csv = pd.read_csv(file, sep=',', encoding='gbk') print(csv)
Run this code, you will find csv is a DataFrame object. All data is contained in csv.
If you have encountered UnicodeDecodeError, you can read this tutorial to fix.
Get the data size
How much data is containd in this csv file? To get this size, you can use code below.
size = csv.shape print(size)
The size is: (197, 15), which means this csv file contains 197 rows * 15 columns data.
Get the header name of a csv file
The header name of a csv file is important, which can help us to read data by column.
columns = csv.columns.values print(columns)
The header name of this csv file is:
['日期' '股票代码' '名称' '收盘价' '最高价' '最低价' '开盘价' '前收盘' '涨跌额' '涨跌幅' '换手率' '成交量' '成交金额' '总市值' '流通市值']
Read data by column
We can read csv data by column name, here is an example.
Read data by row
We also can read csv data by row number. Here is an example.
csv.iloc means we will read the first row data. The result is:
['2020-03-23' "'601988" '中国银行' 3.44 3.48 3.43 3.48 3.5 -0.06 -1.7143 0.039 82294152 283847805.0 1012694001870.0 725033371070.0]
If you plan to read second or third row data, you can do like this: