python学习笔记17pandas库的简单使用
pands读取xlsx文件
xlsx1 = pandas.read_excel(
io=r"文件路径",
sheet_name="sheet名", index_col=None, header=None)
print(xlsx.head())
通过*xlsx.head()*函数返回表格的开头部分
0 1 ... 728 729
0 SMILES nAcid ... XLogP Zagreb
1 a 10 ... 4.666 166
2 b 30 ... 5.804 174
3 c 0 ... 2.964 176
4 d 40 ... 6.015 174
[5 rows x 730 columns]
表格文件的索引
首先我们要明白xlsx表格中行和列都是从1开始的,当我们将参数设置为index_col=None, header=None意味着当我们使用,iloc[0,0]这个表格索引函数想要索引第(0,0)个元素时,对应的是xlsx内的位置为第一行第一列的单元
注意下面两段代码的区别
>>>xlsx1 = pandas.read_excel(io=r"文件路径",sheet_name="sheet名", index_col=None, header=None)
>>>print(xlsx.head())
0 1 ... 728 729
0 SMILES nAcid ... XLogP Zagreb
1 a 10 ... 4.666 166
2 b 30 ... 5.804 174
3 c 0 ... 2.964 176
4 d 40 ... 6.015 174
[5 rows x 730 columns]
>>>print(xlsx1.iloc[0,0])
SMILES
>>>xlsx1 = pandas.read_excel(io=r"文件路径",sheet_name="sheet名", index_col=0, header=0)
>>>print(xlsx.head())
SMILES nAcid ... XLogP Zagreb
a 10 ... 4.666 166
b 30 ... 5.804 174
c 0 ... 2.964 176
d 40 ... 6.015 174
[5 rows x 730 columns]
>>>print(xlsx1.iloc[0,0])
10
表格文件的保存
with pandas.ExcelWriter('new_name.xlsx') as writer:
M.to_excel(writer, sheet_name='sheet_name')
今天先写到这里,困了,( ̄o ̄) . z Z