python pandas csv 经验教训 [vba视角]

import pandas as pd
filename = "VBOM.csv"
# df = pd.read_csv(filename,encoding = 'utf-8',index_col='PN')
# print(df)
# print(df.size)  #  30
# print(df.shape) # (10,3) - not including index_col and index_row  10 rows, 3 cols
# print(df.index) # index_col and index_col_name
# print(df.columns)   # first row (index)
# print(df.loc['81082150'])  # print indexname row [cannot use number since index def]
# print(df.loc['81082150'][1])  # print indexname row_index cell
# print(df.loc['81082150']['Mill'])  # print indexname row_index label cell
# print(df.loc[:]['Mill']) 
# print(df.loc[:]['Mill'][1]) 

df = pd.read_csv(filename,encoding = 'utf-8')
# print(df)
# print(df.loc[1][1])
# print(df.size)
# print(df.shape)
# print(df.loc[0])# first row
# print(df.loc[0][0])# first cell
# print(df.loc[0]['Mill'])# first row, label column
# print(df.at[0,'PN'])#just get the value
# print(df.iat[0,3])#just get the value
# print(df.loc[2].at['PN'])#just get the value
# print(df.shape[0]) # rowscount
# print(df.shape[1]) # colscount
# print(len(df))# rowscount

从vba到python,没想到最大的障碍竟然是在python中处理表格,总觉得操作起来不像vba那样随心所欲. 其实还是太浮躁了, 没有沉下心来去研究. 今天总算是入门了, 拿来用是明白了.

panda读取csv文件读出来的是DataFrame, 相当于一个二维数组, DataFrame由Series组成,相当于由多个一维数组组成, 至于是行数组还是列数组, 可以自行划分. 在我看来, 由行划分比较多.

 

在看runoob.comPandas CSV 文件 | 菜鸟教程 (runoob.com)的时候,建议先看pandas csv, 再看DataFrame, 最后看Series. 和它的顺序反着来, 从大到小.

 

df = pd.read_csv( filename)

得到DateFrame, 然后通过 df.loc[ row_index] 读取行, 得到Series

row= df.loc[row_index]

对于Series, 通过 series.loc[ index] 读取cell Value

当然, 也可以通过 df.iat[row_index, col_index] 直接读取单元格的值.

具体属性和方法可以看pandas.DataFrame — pandas 1.5.1 documentation (pydata.org)

点进去可以看到每个方法的用例.

 

 

pandas的优势在于 index label, 通过标签直接查值, 以及其他更丰富的功能, 比如lookup, 求最大值, 最小值, 简直相当于excel, 不过需要花时间去掌握, 目前对于我来说, 代码段中的语句已经够用了.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值