
在Python的pandas库中,我们可以使用各种方法来导入外部数据。这些方法包括从CSV文件、Excel文件中导入数据。下面我们将详细介绍如何使用pandas导入这些外部数据。
首先,确保已经安装了pandas库。
安装pandas库的命令如下:
pip install pandas
1、导入CSV文件
import pandas as pd
df=pd.read_csv("d:\pandas_data\CBA数据.csv",encoding='gbk')
df
运行结果如下

2、导入excel文件
# 导入使用的库
import pandas as pd
# 导入文件
df=pd.read_excel("d:\pandas_data\CBA数据.xlsx",sheet_name='球队赛程及比分')
df
运行结果如下:

说明:在导入csv文件和excel都需要使用read_csv和read_excel函数,如果在读取excel文件的时候不显示,需要安装openyxl库。