CSV(Comma-Separated Values,逗号分隔值,有时也称为字符分隔值,因为分隔字符也可以不是逗号),其文件以纯文本形式存储表格数据(数字和文本)。
CSV 是一种通用的、相对简单的文件格式,被用户、商业和科学广泛应用。
读取CSV文件:
read_csv()
eg:
import pandas as pd
df=pd.read_csv('hero_data.csv')
print(df.head(5)) #文件内容太多,我只打印前5行
print("df的数据类型:",type(df))
result:
Hero life magic speed attack armor life_recovery magic_recovery \
0 Pike 600 415 330 60 24 7 8
1 Kasha 554 344 335 62 33 1 0
2 Zoe 526 340 340 56 21 7 6
3 Aoun 565 300 335 59 24 1 2
4 Kai Yin 585 410 340 60 29 2 2
attack_range attack_speed magic_resistance
0 150 1 32
1 500 1 30
2 525 1 30
3 175 335 32
4 175 1 32
df的数据类型: <class 'pandas.core.frame.DataFrame'>
读取返回的是一个DataFrame类型
读取Excel文件
read_excel()
eg:
import pandas as pd
df=pd.read_excel('123.xls')
print(df)
print("df的数据类型:",type(df))
result:
name age height weight
0 xiao 12 152 40
1 liu 13 149 42
2 zhang 14 130 35
3 wang 12 160 45
df的数据类型: <class 'pandas.core.frame.DataFrame'>
读取Excel文件返回的依然是DataFrame文件